Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php input stream size limitations

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"));
like image 895
srowley Avatar asked May 24 '11 05:05

srowley


People also ask

What is input stream in PHP?

php://input is a read-only stream that allows you to read raw data from the request body.

What is PHP upload_ max_ filesize?

The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size. Depending on your host, changing these two PHP variables can be done in a number of places with the most likely being php. ini or . htaccess (depending on your hosting situation).

What is File_get_contents PHP input?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

How do PHP streams work?

PHP Stream IntroductionStreams are the way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior.


1 Answers

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);
like image 178
deceze Avatar answered Sep 23 '22 22:09

deceze