php input stream size limitations

2019-01-17 09:14发布

问题:

I am trying to read a raw input stream from php using php://input. This works for most files, however, files over 4MB are being ignored in the upload. I have set post_max_size and upload_max_size to 20M each thinking it would solve my problem, but it didn't. Is there another php.ini setting that needs to be configured or do I need to do chunking of some sort? If so, how would I go about doing that? Here is the upload.php code:

$fileName = $_SERVER['HTTP_X_FILE_NAME'];
$contentLength = $_SERVER['CONTENT_LENGTH'];

file_put_contents('uploads/' . $fileName, file_get_contents("php://input"));

回答1:

Try stream_copy_to_stream, which directly pumps the content of the input into the file without copying it all into memory first:

$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb');
stream_copy_to_stream($input, $file);
fclose($input);
fclose($file);

Alternative:

$input = fopen('php://input', 'rb');
$file = fopen($filename, 'wb');
while (!feof($input)) {
    fwrite($file, fread($input, 102400));
}
fclose($input);
fclose($file);