5

How can I upload files larger than 2MB in PHP,I searched the Internet and i changed the php.ini file,the line is: "upload_max_filesize = 200M",but I still can't upload even 2 MB file.

What seems to be the problem?

Please help me.Thanks in advance.

Cristina
  • 1,991
  • 3
  • 17
  • 24
Surej
  • 358
  • 1
  • 2
  • 16
  • What your `can't` means? Can you be more specific? Any errors from error_log would be very helpful. – Emmerman Apr 07 '11 at 11:21
  • Are you running on a shared host or is this on your local machine? – JohnP Apr 07 '11 at 11:21
  • 2
    There are **LOADS** of questions about this on Stack Overflow. Did you do a search? – Lightness Races in Orbit Apr 07 '11 at 11:22
  • I am running in single system – Surej Apr 07 '11 at 11:24
  • @Emmerman: Cant means i cant get the output. – Surej Apr 07 '11 at 11:25
  • 1
    @karthick: What does "can't get the output" mean? You need to be _much_ more specific, please. Describe **precisely** what you see, and what steps you took to see it. – Lightness Races in Orbit Apr 07 '11 at 11:33
  • @Tomalak Geret'kal:My problem is:I have if loop in my php code,if file is successfully uploaded means get message as success otherwise not,first i tried files upto 1 MB all i got is success,after i tried to upload 17MB file,got not.I searched in net and changed php.ini file,Still i cant uploadthat 17MB file.. – Surej Apr 07 '11 at 12:29

9 Answers9

10

Once upon a time I'm facing this problem with my WAMP server, and when I search for solution, I stumbled upon to this discussion. So, if someone have the same problem, this is my working solution, I hope this help:

  1. I'm using WAMP stack. By reading your comment above, you are using WAMP stack too. In case you don't know, WAMP server has 2 (two) php.ini (in PHP directory and Apache directory) configuration, one for CLI and the other for Apache itself ( see php.ini on WAMP server ). So, I create info.php to detect which php.ini use by my server, and in my case that is the one in Apache directory ( see Which PHP Ini file does my WAMP webpage uses?).

  2. Open your php.ini that used by your server, and as @Pascal Martin suggested, change upload_max_filesizeand also set post_max_size then restart your server.

  3. Check again your info.php, make sure the value of upload_max_filesize and post_max_size has changed to your desired value.

  4. Restart Apache.

It's worked for me, hope this help.

Community
  • 1
  • 1
Seto
  • 1,234
  • 1
  • 17
  • 33
3

As you guessed, you have to set upload_max_filesize...


But you also have to set post_max_size (quoting) :

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
1

There are also other options which could limit this:

max_input_time = 600
php_value max_execution_time = 600
post_max_size = 200M

(...and restart Apache)

sauerburger
  • 4,569
  • 4
  • 31
  • 42
1

get your php.ini-dist file,

  • edit it to set proper values shown above
  • rename it to php.ini
  • copy it in WINDOWS directory
  • restart Apache
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

To upload a large file ( >5MB), I use the chuck upload method.

/**
 * @param $file
 * @param $fileSize
 * @param $name
 * @return int
 */
public function chunkUpload($file, $fileSize, $applicantID, $name) {
    
    $targetFile     = 'upload/'. $name;
    $chunkSize      = 256; // chunk in bytes
    $uploadStart    = 0;

    $handle = fopen($file, "rb");
    $fp     = fopen($targetFile, 'w');

    # Start uploading
    try {
    
        while($uploadStart < $fileSize) {
        
            $contents = fread($handle, $chunkSize);
            fwrite($fp, $contents);
        
            $uploadStart += strlen($contents);
            fseek($handle, $uploadStart);
        }
    
        fclose($handle);
        fclose($fp);
        
        return 200;
        
    } catch (\Exception $e) {
        return 400;
    }
}
RASEL RANA
  • 2,112
  • 1
  • 15
  • 17
0

To upload the larger file, one needs to change/increase the value of both post_max_size and upload_max_filesize directives from the php.ini file.

upload_max_filesize = 200M post_max_size = 201M

This will increase upload limits on a single file to 200 MB, from the default of 2 MB.

PGOEL
  • 566
  • 4
  • 12
0

Put the following code in side the .htaccess file and save it.

php_value upload_max_filesize 200M
php_value post_max_size 200M
php_value max_input_time 2000
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Mohammad Efazati
  • 4,812
  • 2
  • 35
  • 50
0

Changed the server settings in this way...

memory_limit = 250M //The maximum amount of memory in bytes a script is allowed to allocate.
max_input_time = 600 //The maximum time in seconds a script is allowed to parse input data.
max_execution_time = 600 //The maximum time in seconds a script is allowed to run before it is terminated.

post_max_size = 200M //The maximum size in bytes of data that can be posted with the POST method. Typically, should be larger than upload_max_filesize and smaller than memory_limit.
upload_max_filesize = 100M //The maximum size in bytes of an uploaded file.
codelone
  • 604
  • 8
  • 17
-2

Try setting it with within PHP script (on top)..

ini_set("upload_max_filesize", "255M");
ini_set("post_max_size, "256M");
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • it won't work because it is set too late. PHP script do not handle upload itself. It is being actually called only after upload gets finished. – Your Common Sense Apr 07 '11 at 11:53
  • 3
    @Efazati, `Entry can be set in php.ini, .htaccess or httpd.conf` in > 4.2.3, I didn't know that. Thanks. @Col. Shrapnel, I guess you are right. – Dejan Marjanović Apr 07 '11 at 12:03