Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very large uploads with PHP

I want to allow uploads of very large files into our PHP application (hundred of megs - 8 gigs). There are a couple of problems with this however.

Browser:

  • HTML uploads have crappy feedback, we need to either poll for progress (which is a bit silly) or show no feedback at all
  • Flash uploader puts entire file into memory before starting the upload

Server:

  • PHP forces us to set post_max_size, which could result in an easily exploitable DOS attack. I'd like to not set this setting globally.
  • The server also requires some other variables to be there in the POST vars, such as an secret key. We'd like to be able to refuse the request right away, instead of after the entire file is uploaded.

Requirements:

  • HTTP is a must.
  • I'm flexible with client-side technology, as long as it works in a browser.
  • PHP is not a requirement, if there's some other technology that will work well on a linux environment, that's perfectly cool.
like image 422
Evert Avatar asked May 14 '09 17:05

Evert


People also ask

What is the maximum file upload size in PHP?

The default PHP values are 2 MB for upload_max_filesize, and 8 MB for post_max_size.

How can I upload 10 MB file in PHP?

Two PHP configuration options control the maximum upload size: upload_max_filesize and post_max_size . Both can be set to, say, “10M” for 10 megabyte file sizes. However, you also need to consider the time it takes to complete an upload.

How can I upload more than 2 MB in PHP?

by default PHP will not handle file uploads larger than 2MB, if one requires PHP to handle larger files then one must set upload_max_filesize and post_max_size in your php. ini file to be larger than 2MB.

How do I handle large uploads?

Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable file uploads. Chunking is the most commonly used method to avoid errors and increase speed.


2 Answers

upload_max_filesize can be set on a per-directory basis; the same goes for post_max_size

e.g.:

<Directory /uploadpath/>   php_value upload_max_filesize 10G   php_value post_max_size 10G </IfModule> 
like image 148
Frank Farmer Avatar answered Oct 03 '22 00:10

Frank Farmer


Python Handler?

Using a Python POST handler instead of PHP. Generate a unique identifier from your PHP app that the client can put in the HTTP headers. With mod_python to reject or accept the large upload before the entire POST body is transmitted.

I think http://www.modpython.org/live/current/doc-html/dir-handlers-hph.html

Allows you to check headers and decline the rest of the POST input. I haven't tried it but might be the right path?

Looking at the source of mod_python, the buffering of the input via read() seems to allow bit-at-a-time evaluation of the HTTP input. Headers are first.

https://svn.apache.org/repos/asf/quetzalcoatl/mod_python/trunk/src/filterobject.c

like image 24
Aiden Bell Avatar answered Oct 03 '22 02:10

Aiden Bell