Can file uploads time out in PHP? - php

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!

You need a proper value for the following php.ini settings:
max_input_time (not max_execution_time!)
upload_max_filesize
post_max_size
and maybe
memory_limit

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.

I was able solve this problem using the following settings, you could use different values but you get the idea:
For my server, I put these lines in a ".user.ini" file inside the script directory, your server may look for a different file, if you do a phpinfo('user_ini.filename') on the server it will spit out the file you need to put your values in
max_execution_time = 1800
max_input_time = -1
post_max_size = 100M
upload_max_filesize = 100M
memory_limit = 256M

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.

If changing any of the above parameters doesn't seem to make any difference, it can be that a html form somewhere contains the name MAX_FILE_SIZE as a hidden field.
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
In the example above, any file over 10MB will not be uploaded.

Related

More then 8mb file is not upload in wordpress

I am try to uploading file on WordPress server.after 8 mb uploading file is break.There are three ways to increase the size of the uploading file.
PHP.ini (Changing the settings of PHP.ini file)
htaccess (Also change the settings in htaccess file but still of no use)
changing settings in wp-admin file.
all are not working.
Is it any other way to increase the size of the uploading file.
You have to configure this two things in php.ini upload_max_filesize and post_max_size then restart your webserver
Apart from the maximum filesize setting, try to look and configure max_execution_time and post_max_size in the php.ini if necessary. Then,restart Apache.
It depends on where you are uploading file ie. On which site.
What is your connection bandwidth.
How much uploading your WordPress service provider supporting.
Your script can have a limited set of time to be executed.
Try set the request timeout inside the php script with set_time_limit() (for a test on time limit, not for an upload)
Often, when the limit is represented by the size of file, you should have a message warning before the upload starts. In this case the server is letting you upload, so in most cases it's not a size problem.
Take into account also that some providers are imposing execution time from web server setup so you have to check this too. If I were you I'll try to execute a script that does nothing (wait) for some minutes, and see if that the time the request goes in timeout is the same for uploading a file.

Increasing the Upload Limit of my PHP script

I have a simple PHP script. Its used to upload users into the joomla tables. Basically it uploads users into joomla three main tables.
The PHP uploader sript works fine when the CSV is about 80MB to 100 MB.
It does not work or just nothing happens when the file size is 500MB or above.
How do i make the PHP script work to upload the CSV files of over 500 MB?
Do i actually change something in my PHP.ini settings or is there something else i could
add in the script itself.
Thanks in Advance.
in the .htcaccess file add the following:
php_value upload_max_filesize 500M
php_value post_max_size 500M
in php.ini add:
upload_max_filesize = 500M
post_max_size = 500M
You have to change the upload_max_filesize in php.ini
I had the same issue long time ago with rails and mysql,
You have to consider 3 things when you want to upload a file:
Max upload file in PHP
Be sure that MySQL will save a big file.
Your browser will lost connection after a while uploading a file to your database if it don't receive any answer from the server.
I think that You handled the first 2, but to handle the 3rd probably you will need a upload progress bar to keep the session active. Actually you need some AJAX to keep the server and the client "talking" meanwhile the file still uploading.

How can I prevent large Apache/PHP file uploads from failing?

I recently installed a PHP download portal on one of our servers. Everything is working fine, but users can't upload large files (~ 20 MB).
I set the following settings to extremely large values or unlimited:
memory_limit
upload_max_filesize
post_max_size
memory_limit
Full php.ini here: http://pastebin.com/NrqJvMVM
But it still fails after restarting the server.
Am I missing a setting? Do I have to update any values in the Apache configuration? Could a company firewall somehow interfere with that?
EDIT: I checked phpinfo() and the master configuration still shows the old values. The config file C:\Windows\php.ini however, has the new values. Am I using the wrong config file.
You can test the params in a script, too. Perhaps this helps:
max_execution_time = ini_set("max_execution_time", "-1");
post_max_size = ini_set("post_max_size", "200M");
memory_limit = ini_set("memory_limit", "200M");
upload_max_filesize = ini_set("upload_max_filesize", "200M");
In .htaccess:
php_value upload_max_filesize 200M
php_value memory_limit 256M
php_value max_execution_time 18000
Here are nice articles about this topic: http://www.cyberciti.biz/faq/linux-unix-apache-increase-php-upload-limit/ | https://www.dokuwiki.org/faq:uploadsize
Is Suhosin installed on your server or do you use fcgi ;-)?
All the configuration settings for your installation are contained in the php.ini file. Sometimes these setting might be overridden by directives in apache .htaccess files or even with in the scripts themselves.
Following settings that we need to modify -
file_uploads
upload_max_filesize
max_input_time
memory_limit
max_execution_time
post_max_size
The configuration settings in detail below.
upload_max_filesize and post_max_size
Files are usually POSTed to the webserver in a format known as 'multipart/form-data'. The post_max_size sets the upper limit on the amount of data that a script can accept in this manner. Ideally this value should be larger than the value that you set for upload_max_filesize.
It's important to realize that upload_max_filesize is the sum of the sizes of all the files that you are uploading. post_max_size is the upload_max_filesize plus the sum of the lengths of all the other fields in the form plus any mime headers that the encoder might include. Since these fields are typically small you can often approximate the upload max size to the post max size.
According to the PHP documentation you can set a MAX_UPLOAD_LIMIT in your HTML form to suggest a limit to the browser.
memory_limit
When the PHP engine is handling an incoming POST it needs to keep some of the incoming data in memory. This directive has any effect only if you have used the --enable-memory-limit option at configuration time. Setting too high a value can be very dangerous because if several uploads are being handled concurrently all available memory will be used up and other unrelated scripts that consume a lot of memory might effect the whole server as well.
max_execution_time and max_input_time
These settings define the maximum life time of the script and the time that the script should spend in accepting input. If several mega bytes of data are being transfered max_input_time should be reasonably high. You can override the setting in the ini file for max_input_time by calling the set_time_limit() function in your scripts.
Additonal Comments
Apache Settings
The apache webserver has a LimitRequestBody configuration directive that restricts the size of all POST data regardless of the web scripting language in use. Some RPM installations sets limit request body to 512Kb. You will need to change this to a larger value or remove the entry altogether.
Other Options
If you expect to handle a large number of concurrent file transfers on your website consider using a perl or java server side component. PHP happens to be our favourite web programming language as well but perl and Java are just slightly ahead when it comes to file upload.
See also:
Upload large File upto 100MB using php
upload large files using php, apache
PHP: Uploading large files fail
Maybe, this article can help you. You can add params to .htaccess
You have written post_max_size variable twice and out of which value of first if 0 and value of second is 9999M, remove the first one and try it.
Am I using the wrong config file.
Well, phpinfo() tells you which one you are using. Look at Configuration File (php.ini) Path and Loaded Configuration Fileright near the top of the output.
Maybe MAX_FILE_SIZE is not properly set...
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<input name="filedata" type="file" />
<input type="submit" value="Send file" />
</form>
You should find correct php.ini file =)
I have been in your situation in the past and the best solution I found was using third party library to split the files into chunks on the client side and put it all back together on the server.
I found this: http://www.plupload.com/
Very handy and effective. It works with a variety of different technologies so it can work on a wide majority of clients machines. (flash, silverlight, html5 file api aso...)
Cheers!

move_uploaded_file file disappears from tmp but does not reach destination

I have a really odd problem. I am using an upload form to upload videos. Sometimes I have to try twice to upload a file so I know it works but these files take a long time to upload so I don't want the end-user getting mad if the process fails. Also, this works 100% of the time on my test machine so I am thinking there is a config problem.
The file is 330mb and I set upload_max_filesize and post_max_size to 500mb. The max_execution_time and max_input_time are set to 60000 for testing purposes. memory_limit is what I think may be the problem. It is set to 128mb. Does it need to be higher to have a consistent upload success rate? Anybody know of any other problems that could cause things to go wrong?
You're right in assuming memory_limit is your culprit.
Taken from php.net.
post_max_size (int)
Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize.
If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. (...)

image upload in PHP

I can upload small size files with no problem,
but fail when the size is more than 1M.
What's the matter?
You probably need to configure the upload_max_filesize directive, in your php.ini file : PHP will not accept an upload of a file that is bigger than what this directive defines.
And note that you might also need to adjust post_max_size -- which defines the total size of data that can be POSTed (And files upload are sent via POST).
You can also take a look at the Handling file uploads section of the manual, which can give you a couple of useful informations about files upload.
Are you sure you have upload_max_filesize set correctly in php.ini?
Edit you php.ini file to allow for larger uploads.
HERE's some info
You can call echo phpinfo() and then verify your upload_max_filesize and other php environment settings. Its very possible that your script is dying because one of the max limits is being exceeded.
Depending on your environment you can either use ini_set() to change the necessary values at run-time or you can simply edit your php.ini file to set the value permanently. Please note that not all php.ini settings can be changed at run time and if you do edit php.ini, you will need to restart Apache.
As said by others check your php.in for upload_max_filesize and post_max_size settings. If they are okay and if you are using a 3rd party script for uploading, make sure the script is not limiting the max file size by doing something like:
if( $_FILES["file"]["size"] > (1024 * 1024) ) // disallow uploads > 1MB
{
// max size exceeded.
}

Categories