简体   繁体   中英

upload large file in php

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.

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_filesize and 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.

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 .

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)

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

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.

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

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;
    }
}

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.

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

ini_set("upload_max_filesize", "255M");
ini_set("post_max_size, "256M");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM