PHP

ID #219

I can't upload a file larger than 8MB through a PHP script

Applies to: Grid System

 

If you have php error logging with reporting set to E_ALL then you will see the PHP Error reported:

PHP Warning: POST Content-Length of 9470478 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

The problem is unset or incorrect settings in your PHP configuration file.

To allow file uploads larger than the default 8 megabytes, put these settings in your php.ini:

post_max_size = 16M
upload_max_filesize = 16M


You can check phpinfo() output to make sure that those values get set correctly.

Also make sure that your script does not have an html form input tag for MAX_FILE_SIZE that is smaller (in bytes) than the size of the files that you are trying to upload.

Now you will be able to upload files up to 16 megabytes.

NOTE: although PHP.net lists post_max_size as being a PHP_INI_PERDIR, because of the way we run PHP in CGI, this cannot be changed in your .htaccess file. However, there is a workaround:

Copy your current php.ini file into the directory that you require the custom post_max_size in.

**THE NEXT STEP IS VERY IMPORTANT**

Obviously, we don't want the outside world to have access to your settings in your php.ini file. The solution? Drop an .htaccess file in the same directory with the following lines in it:

        <Files ~ "\.ini$">
        Order allow,deny
        Deny from all
        </Files>


      SetEnv PHPRC .

This will deny any web requests to any file in that directory ending in .ini; thus preventing your PHP settings from being seen by others.

Now you have a secure php.ini file that you can modify settings in to affect the .php files within that directory.

For a test upload script that works, see this other FAQ


Last update: 2011-01-15 12:34
Author: FAQ Admin
Revision: 1.4

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 3.82 (17 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
If you are trying to stuff a big file into MySQL, MySQL has it's own blob size limit set that you can't alter, which is 4MB
Added at: 2003-01-28 18:04

Comment of Anonymous:
Check the memory_limit variable as well. For certain scripts (squirrelmail for example) this is the limiting value.
Though changing it seems a dubious solution.
Added at: 2004-02-26 12:42

Comment of Anonymous:
Make sure php sees the php.ini file - check "Configuration File (php.ini) Path" entry in phpinfo()'s output and copy php.ini to that directory.
Added at: 2005-06-24 12:50