File_get_contents or fopen to read multiple MB php: // input?

This may be a repeating question, but it is not: I get a few megabytes of data through php: / input (1-500mb), which I need to save to a file. More features (server load, speed) using:

file_put_contents($filename, file_get_contents('php://input')) 

OR

 $input = fopen("php://input", "r"); $temp = tmpfile(); $realSize = stream_copy_to_stream($input, $temp); fclose($input); $target = fopen($filename, "w"); fseek($temp, 0, SEEK_SET); stream_copy_to_stream($temp, $target); fclose($target); 
+4
source share
1 answer

There is an even shorter version: copy

  copy("php://input", $filename); 

PHP already internally implements what your code does. (Not sure if that would measure the difference). Although I'm not sure why you first created a temporary file.

And if the input file is up to 500 MB, then the file_get_contents approach will not work in any case, since it had to store all this data in line / memory.

+6
source

All Articles