Can file uploads time out in PHP?

Viewed 77520

Hi im quite new to PHP, i have created a form for very large csv files to be uploaded to my server. Some one mentioned to me that the browser can time out due to the uploading file being to big, is this true? and if so, can it be prevented?

Thanks for your help!

10 Answers

There are some configuration directives that can cause large uploads to fail if their values are too small:

PHP

  • max_input_time   Maximum time in seconds a script is allowed to parse input data, like POST, GET and file uploads
  • upload_max_filesize   Maximum size of an uploaded file.
  • post_max_size   Maximum size of post data allowed.

Apache

  • TimeOut   Amount of time the server will wait for certain events before failing a request
  • LimitRequestBody   Restricts the total size of the HTTP request body sent from the client

There are probably some more than this.

A good way to work around the poor handling of large file uploads in php, is to use an uploader like JUpload which will split the file into chunks before sending them. This also has the benefit for your users that they get a proper progress feedback while uploading, and they can upload multiple files in one go.

When uploading very large files, you have to change 4 configuration variables:

  • upload_max_filesize
  • post_max_size
  • memory_limit
  • time_limit

Time limit may be increased at runtime with set_time_limit().

A script is allowed to run, by default, for something like 30 seconds. You can use the set_time_limit() function to alter this. Also, if your user will need to upload large files, you'll need to change the post_max_size and/or the upload_max_filesize values in your php.ini file.

Also, if you want to just extend your timeout limit globally, you can change max-execution-time in php.ini.

Yes it is true. File upload is done through a POST request and requests in general are subject to timeout. You should be able to reconfigure your environment for a longer request timeout.

It's not just timeouts that can cause problems. There are some limits on the maximum size of file that can be uploaded. These limits can be changed in the php.ini file:

post_max_size
upload_max_filesize memory_limit

Check out http://uk.php.net/ini.core for details.

My answer is not directly related to your original question, but if you have a reverse proxy load balancer in front of your PHP script, the load balancer can timeout or block large uploads. Always check your load balancer's configuration if you support file uploads. Just like PHP, most load balancers default settings for uploads are pretty small.

Related