Hướng dẫn php download large file

To download large files from server, I have changed the below settings in php.ini file:

Upload_max_filesize  - 1500 M
Max_input_time  - 1000
Memory_limit    - 640M
Max_execution_time -  1800
Post_max_size - 2000 M

Now, I am able to upload and download 175MB video on server. Since, I have the dedicated server. So, making these changes were easy.

Below is the PHP script to download the file. I have no made any changes in this code snippet for large file size.

// Begin writing headers

            ob_clean(); // Clear any previously written headers in the output buffer

            if($filetype=='application/zip')
            {
                if(ini_get('zlib.output_compression'))
                    ini_set('zlib.output_compression', 'Off');
                $fp = @fopen($filepath, 'rb'); 
                if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
                {
                    header('Content-Type: "$content_type"');
                    header('Content-Disposition: attachment; filename="'.$filename.'"');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header("Content-Transfer-Encoding: binary");
                    header('Pragma: public');
                    header("Content-Length: ".filesize(trim($filepath)));
                }
                else
                {
                    header('Content-Type: "$content_type"');
                    header('Content-Disposition: attachment; filename="'.$filename.'"');
                    header("Content-Transfer-Encoding: binary");
                    header('Expires: 0');
                    header('Pragma: no-cache');
                    header("Content-Length: ".filesize(trim($filepath)));
                }

                fpassthru($fp);
                fclose($fp);
            }
            elseif($filetype=='audio'|| $filetype=='video')
            { 
                global $mosConfig_absolute_path,$my;
                ob_clean();
                header("Pragma: public");
                header('Expires: 0');
                header('Cache-Control: no-store, no-cache, must-revalidate');
                header('Cache-Control: pre-check=0, post-check=0, max-age=0');
                header("Cache-Control: public");    
                header("Content-Description: File Transfer");
                header("Content-Type: application/force-download"); 
                header("Content-Type: $content_type");
                header("Content-Length: ".filesize(trim($filepath)));
                header("Content-Disposition: attachment; filename=\"$filename\"");
                // Force the download           
                header("Content-Transfer-Encoding: binary");            
                @readfile($filepath);
            }
              else{ // for all other types of files except zip,audio/video
                ob_clean();
                header("Pragma: public");
                header('Expires: 0');
                header('Cache-Control: no-store, no-cache, must-revalidate');
                header('Cache-Control: pre-check=0, post-check=0, max-age=0');
                header("Cache-Control: public");    
                header("Content-Description: File Transfer");
                header("Content-Type: $content_type");
                header("Content-Length: ".filesize(trim($filepath)));
                header("Content-Disposition: attachment; filename=\"$filename\"");
                // Force the download           
                header("Content-Transfer-Encoding: binary");            
                @readfile($filepath);       
            }
            exit;


To download large files through PHP script, the code is as follows −

Example

Output

This will produce the following output −

The large file will be downloaded.

The function ‘readfile_chunked’ (user defined) takes in two parameters- the name of the file and the default value of ‘true’ for the number of bytes returned meaning that large files have been successfully downloaded. The variable ‘chunksize’ has been declared with the number of bytes per chunk that needs to be read. The ‘buffer’ variable is assigned to null and the ‘cnt’ is set to 0. The file is opened in binary read mode and assigned the variable ‘handle’ .

Until the end of file of the ‘handle’ is reached, the while loop runs and reads the contents of the file based on the number of chunks that need to be read. Next it is displayed on the screen. If the value of ‘retbytes’ (the second parameter to the function) is true, the length of the buffer is added to the ‘cnt’ variable. Otherwise, the file is closed and the ‘cnt’ value is returned. In the end, the function returns the ‘status’.

Hướng dẫn php download large file

Updated on 09-Apr-2020 11:37:51

  • Related Questions & Answers
  • How to upload large files above 500MB in PHP?
  • Download file through an AJAX call in PHP
  • How to stream large .mp4 files in HTML5?
  • How to Handle Large CSV files with Pandas?
  • How to download all pdf files with selenium python?
  • How we can compress large Python files?
  • Sending large files over internet
  • How to force file download with PHP?
  • How to scrape through Media Files in Python?
  • How to download APK files from Google Play Store on Linux
  • How to use chrome webdriver in Selenium to download files in Python?
  • How to split or break large files into pieces in Linux?
  • How to Download and Extract Tar Files with One Command in Linux
  • Splitting and uploading extremely large files to Amazon S3
  • How to sort a large number of csv files in ascending order in R?