Skip to content

Instantly share code, notes, and snippets.

@danielbeardsley

danielbeardsley/README.md

Last active Feb 1, 2021
Embed
What would you like to do?
Unlimited size uploads in PHP: a minimal demonstration.

Unlimited upload size in PHP

a minimal implementation

A demonstration of accepting unlimited-size uploads in PHP. The included server.php pipes the POST body directly to /dev/null and returns a JSON encoded hash of some useful stats. Using this method (streaming from stdin) we are able to accept uploads of any size with little memory overhead.

Setup

Symlink apache config and reload

$ sudo ln -s ~+/apache-upload.conf /etc/httpd/conf.d/`
$ sudo service httpd reload  

Running

Upload 10MB to our test server:

$ ./upload.sh 10
{"content-length header":"10485760","upload-size":10485760,"memory":234816}
<VirtualHost *:1234>
DocumentRoot /tmp/php-upload-test
<Directory /tmp/php-upload-test>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
Listen 1234
<?
$result = [
'content-length header' =>
isset($_SERVER["CONTENT_LENGTH"]) ? $_SERVER["CONTENT_LENGTH"] : null
];
$output = fopen("/dev/null", "w");
$input = fopen("php://input", "r");
$result['upload-size'] = stream_copy_to_stream($input, $output);
fclose($input);
fclose($output);
$result['memory'] = memory_get_peak_usage();
echo json_encode($result) . "\n";
die;
#/bin/sh
# First argument is POST body size in MB
size_mb="$1"
size_blocks=$(($size_mb * 2048))
dd if=/dev/zero count=$size_blocks 2>/dev/null | tr '\000' 'X' |
curl --silent --data-binary @- http://127.0.0.1:1234/server.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment