• You are not logged in. | Login

Post a reply

November 17, 2006 6:58 am

biopd42
Member
Ranks

read a really big file (20 or 30 megs)

In my site I use PHP code to read the output of another program. It writes it's output in temporary files, which I read using file_get_contents() functions.

My problem is that I read big files (for files close to 20-30MBs for example), and it's very very slow and the servers memory fills right up.

Please note that there is no other way for me to do this because this program, by default, write it's output to a text file, so I can't read the output on the fly. The output file is first created and then I read it into a string and parse it according to what I need.

How can I make the php read the file faster?


 

 

November 17, 2006 6:41 pm

phppat
Member
Ranks

Re: read a really big file (20 or 30 megs)

If the text yoiu're reading uses line breaks, you can easily read the file one line at a time, processing each line.  This saves you from having to load the entire file contents into memory with file_get_contents();

Try this:

$fp = fopen ("path/to/file.txt", 'r');

while ($line = readline ($fp) && ! feof($fp)) {
   
   ...  code to process each $line ...
  
  unset($line);
};

PHP monster

 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services