tags:

views:

64

answers:

2

Hello,

I have a rather big .txt file (~220Mb) and I need to read it by 100 lines (\n symbol) chunks (for example). How can I do it using php?

Thank you.

+1  A: 

fopen and fgets. The fgets manual page has an example on reading a file line-by-line without loading it into memory all at once.

Matti Virkkunen
A: 
$fp = open('big_text_file.txt',"r");
if($fp){
    $c = 0;
    $data = array();
    while(!feof($fp)){
        if($c == 100){
            $c = 0;
            // Do whatever it is you want here
            unset($data);
            $data = array();
        }
        $data[] = fgets($fp,4096);        
        $c++;
    }
    if($c > 0){
        // Do whatever you need to again
    }
    fclose($fp);
}
GWW