Friday, December 5, 2008

PHP read large file line by line?

PHP read large file line by line?

how to read a large php file line by line?

fread can't?


What if the file is larger than memory_limit (8Mb)?

fgets() is the right answer in this case.

So, instead of coding with file() which works now with a 200K file,
start with fgets and your script will not break when the file grows.

Resource Limits

Resource Limits Name Default Changeable Changelog
memory_limit "128M" PHP_INI_ALL "8M" before PHP 5.2.0, "16M" in PHP 5.2.0

Here's a short explanation of the configuration directives.

memory_limit integer

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using -enable-memory-limit in the configure line. This was also required to define the functions memory_get_usage() and memory_get_peak_usage().
When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

See also: max_execution_time.

Depends on what you're trying to do. If you just want to know if the files are different from one another you can use the md5_file() on each of the files and compare the md5 hash.

If your 10GB files contain data you want to compare individually, such as a CSV, you could always create a temporary MySQL table to house the CSV contents for both of the files and then import the CSV files into MySQL from the command line. (In your temp table you would need to identify the contents of the first file to the second -- such as assigning an ID of 1 to every record that is part of your first CSV.) Then it's just a matter of comparing the data between the two tables with vanilla SQL. You're not limited with PHP at all at that point.

The reason I'm here is I'm trying to find a solution for the filesize() issue. I have a file that is 11GB big and the return value of filesize() only represents about 2.7GB of the file. I need to be able to compare the file size of one file to another and give back the difference between the two files in MB. If anyone has a solution for that problem I'd greatly appreciate an answer. I think the solution is using exec('dir \\externalhost\path\to\file.ext') to get the number of bytes, but I still have the issue of the number of bytes being greater than the amount PHP will support when calculating the difference...

No comments: