How to Save Memory When Reading a File in PHP

How to save memory when reading a file in Php?

Unless you know the offset of the line, you will need to read every line up to that point. You can just throw away the old lines (that you don't want) by looping through the file with something like fgets(). (EDIT: Rather than fgets(), I would suggest @Gordon's solution)

Possibly a better solution would be to use a database, as the database engine will do the grunt work of storing the strings and allow you to (very efficiently) get a certain "line" (It wouldn't be a line but a record with an numeric ID, however it amounts to the same thing) without having to read the records before it.

Reading file PHP using too much memory

This :

 $data = file_get_contents($filePath);

Is way to heavy for big files.

This is how you read a file line by line :

$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}

fclose($handle);
} else {
// error opening the file.
}

Save file to memory and later write back

Provided this is all done withing the same request, then yes you can.
Just save the file contents to a variable, then write it back again:

$temp = file_get_contents('path/to/file.ext');

className::emptyDir($dir);

file_put_contents('path/to/file.ext', $temp);

Why does reading from a php://memory wrapper I've just written to fail?

You'll need to rewind($file_handle) after writing before you can read what you've just written, because writing moves the file pointer to the end of the file

Reading very large files in PHP

Are you sure that it's fopen that's failing and not your script's timeout setting? The default is usually around 30 seconds or so, and if your file is taking longer than that to read in, it may be tripping that up.

Another thing to consider may be the memory limit on your script - reading the file into an array may trip over this, so check your error log for memory warnings.

If neither of the above are your problem, you might look into using fgets to read the file in line-by-line, processing as you go.

$handle = fopen("/tmp/uploadfile.txt", "r") or die("Couldn't get handle");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
// Process buffer here..
}
fclose($handle);
}

Edit

PHP doesn't seem to throw an error, it just returns false.

Is the path to $rawfile correct relative to where the script is running? Perhaps try setting an absolute path here for the filename.



Related Topics



Leave a reply



Submit