Thursday, March 22, 2012

How to Upload Large Files in PHP


Uploading a file from a web form in PHP is easy. The online manual provides a Handling File Uploads section, and there are several articles on sitepoint.com, including How To Handle File Uploads With PHP by Kevin Yank.

One of the most popular uses is image uploads. Your users can submit photographs from a form without resorting to FTP or other convoluted methods. HTML5 and Flash also permit drag and drop, so the operation is likely to become easier as browsers evolve.

This is where the problems can begin. Camera manufacturers continually brag that they have a larger set of megapixels than their competitors. It’s all rubbish, of course — unless you’re a professional photographer or need to print extremely large images, anything over 4MP is fairly pointless and lens quality is much more important. However, even low-end compacts have 12MP and mobile phones have more than 5MP. The result is that a typical snapshot can easily be 6MB in size.

By default, PHP permits a maximum file upload of 2MB. You can ask users to resize their images before uploading but let’s face it: they won’t. Fortunately, we can increase the limit when necessary.


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. PHP scripts normally time-out after 30 seconds, but a 10MB file would take at least 3 minutes to upload on a healthy broadband connection (remember that upload speeds are typically five times slower than download speeds). In addition, manipulating or saving an uploaded image may also cause script time-outs. We therefore need to set PHP’s max_input_time and max_execution_time to something like 300 (5 minutes specified in seconds).

These options can be set in your server’s php.ini configuration file so that they apply to all your applications. Alternatively, if you’re using Apache, you can configure the settings in your application’s .htaccess file:

php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_input_time 300
php_value max_execution_time 300

Finally, you can define the constraints within your PHP application:

ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);


PHP also provides a set_time_limit() function so you don’t need to set max_execution_time directly.

Setting the options in your PHP code is possibly more practical, since you can extend the execution time and increase the file size when your application is expecting a large upload. Other forms would revert to the default 30-second time-out and 2MB limit.

Do you have any other tips for uploading large files in PHP?



If "large files" (ie: 50 or 100 MB) fail, check this:

It may happen that your outgoing connection to the server is slow, and it may timeout not the "execution time" but the "input time", which for example in our system defaulted to 60s. In our case a large upload could take 1 or 2 hours.

Additionally we had "session settings" that should be preserved after upload.

1) You might want review those ini entries:

* session.gc_maxlifetime
* max_input_time
* max_execution_time
* upload_max_filesize
* post_max_size

2) Still fails? Caution, not all are changeable from the script itself. ini_set() might fail to override.

More info here:
http://www.php.net/manual/es/ini.list.php

You can see that the "upload_max_filesize", among others, is PHP_INI_PERDIR and not PHP_INI_ALL. This invalidates to use ini_set():
http://www.php.net/manual/en/configuration.changes.modes.php

Use .htaccess instead.

3) Still fails?. Just make sure you enabled ".htaccess" to overwrite your php settings. This is made in the apache file. You need at least AllowOverride Options.

See this here:
http://www.php.net/manual/en/configuration.changes.php

You will necessarily allow this manually in the case your master files come with AllowOverride None.

Conclussion:

Depending on the system, to allow "large file uploads" you must go up and up and up and touch your config necessarily up to the apache config.

Sample files:

These work for me, for 100MB uploads, lasting 2 hours:

In apache-virtual-host:
-----------------------------------------------------------

    AllowOverride Options

-----------------------------------------------------------

In .htaccess:
-----------------------------------------------------------
php_value session.gc_maxlifetime 10800
php_value max_input_time         10800
php_value max_execution_time     10800
php_value upload_max_filesize    110M
php_value post_max_size          120M
-----------------------------------------------------------

In the example,
- As I last 1 to 2 hours, I allow 3 hours (3600x3)
- As I need 100MB, I allow air above for the file (110M) and a bit more for the whole post (120M).



I’ve seen that many of my friends are struggling with the uploads of the bigger or larger files in PHP. After looking at their struggle, i’m here to solve the problem of uploading larger or bigger files in PHP.
Most of the web servers are configured such a way that a user can only upload the maximum file size of 2MB. So there might be the problem for the people who wants to upload the .pdf file of size around 15MB. But, you can increse the maximum upload file size limit by using .htaccess file.
Here is a small tips for you which you can use to upload such a large file using file field of the form and move_uploaded_file() function in PHP.
1) Create a .htaccess file in the root folder of web server.
2) Put the following code in side the .htaccess file and save it.

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200



Now you can upload the file-size up-to 20MB in a simple way using file field in your html form and move_uploaded_file() function available in PHP. In the above .htaccess file, uploading capability is increased by the four parameter first one is maximum file size for uploading, second one is maximum size of the post data , third one is maximum time in seconds a script is allowed to run before it is terminated by the parser and last one is maximum time in seconds a script is allowed to parse input data such as like file uploads, POST and GET data.


You can change the above parameter to upload the bigger file size than 20MB.

------------------------------------------------------------------------------------------------------------------------------------------------------------------


You’re assuming that the server is configured to honor what you override via .htaccesss, Apache’s Allow Options has to be set so you can do so.

You’ll also want post_max_size to be a little bigger than upload_max_filesize if you have other fields in the form. You’ll want it a lot bigger if you have multiple file upload fields in one form.
Finally, once you get all of that sorted, you might still run into Apache’s LimitRequestBody directive if its been set in your server config. This limits the max post size Apache will allow from any POST (not just PHP).
Passing 0 to max_execution_time implies unlimited script execution time.

------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Goto php.ini file,inside php folder.
2. search for variables ‘upload_max_filesize’ & ‘post_max_size or max_post_size’
3. change its default value from 2M to 10M(or depends on ur need ).
4. save the php.ini file.
5. restart the server



------------------------------------------------------------------------------------------------------------------------------------------------------------------




Debugging tips for people who are unable to get it to work:
1) Inside the directory that contains the php file upload handler script, create a php file that contains a call to phpinfo(). Open this file in browser and inspect the “local” values (e.g. upload_max_filesize) reported by phpinfo.
2) Activate additional error logging in php if possible. If you do not have access to php’s global configuration file, you *might* be able to override settings via .htaccess by aadding these lines:
# enable sending error message to browser
php_flag display_errors On
# shows most errors including NOTICEs
php_value error_reporting 2047
3) Have a look at apache error log for errors related to apache, not php. Apache error logs are enabled by default and usually stored in a file called ‘error.log’. This gives you further details about configuration errors such as:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
4) Try entering numerical values instead of php constants. Example:
20M => 20971520
100M => 104857600



------------------------------------------------------------------------------------------------------------------------------------------------------------------





Check what is the server type.If the server is windows server it works with .htaccess file but produces internal server error for the Linux server.
For this you have to create the custom php.ini file and upload in the root folder.
Create the php.ini file and put
upload_max_filesize = 100M
post_max_size = 100M
output_buffering = on
max_execution_time = 1000
max_input_time = 1000
memory_limit = 64M
I think it works.




http://www.jumploader.com/download.html




http://codestips.com/php-ftp-upload-tutorial/

http://www.jonasjohn.de/snippets/php/ftp-example.htm

No comments: