Search code examples
phpinputuploadinputstream

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"));

Solution

  • 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);