albanx
albanx

Reputation: 6335

file_get_contents or fopen for reading several MB of php://input?

This might seem to be a repeated question, but it is not: I receive several megabytes of data via php:/input (1-500mb) that I have to save in a file. Is more perfomance-wise (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);

Upvotes: 4

Views: 2020

Answers (1)

mario
mario

Reputation: 145482

There is a shorter version still: copy

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

PHP already internally implements what your code does. (Not sure it would make a measurable difference). Albeit I'm uncertain why you'd create a temporary file first.

And if the input file is up to 500 MB, then the file_get_contents approach wouldn't work anyway, as it had to keep all that data in a string / memory.

Upvotes: 6

Related Questions