A recent customer reported that all the images on their website could not be downloaded, and all the downloaded images showed that the files were damaged, which caused them to be unable to be opened. The author tested and found that this problem exists, after carefully reading the source code, the root cause of the problem is the fread function. The second parameter of the fread function is to set the maximum number of bytes to read, experiments show that the fread function can read a maximum of 8192 bytes at a time, that is, 8 kB,
A recent customer reported that all the images on their website could not be downloaded, and all the downloaded images showed that the files were damaged, which caused them to be unable to be opened. The author tested and found that this problem exists, after carefully reading the source code, the root cause of the problem is the fread function. The second parameter of the fread function is to set the maximum number of bytes to read, experiments show that the fread function can read a maximum of 8192 bytes at a time, that is, 8 kB. if you want to read a file larger than this size, you need to read it cyclically until the end of the file. Based on the above corrections, the following code is the code after the rest, and the problem is solved by testing.
Function download ($ file_url, $ new_name = '') {if (! Isset ($ file_url) | trim ($ file_url) = '') {return '000000';} if (! File_exists ($ file_url) {// check whether the file has a return '000000';} $ file_name = basename ($ file_url); $ file_type = explode ('. ', $ file_url); $ file_type = $ file_type [count ($ file_type)-1]; $ file_name = trim ($ new_name = '')? $ File_name: urlencode ($ new_name ). '. '. $ file_type; // input file tag phpernoteheader ("Content-type: application/octet-stream"); header ("Accept-Ranges: bytes "); header ("Accept-Length :". filesize ($ file_url); header ("Content-Disposition: attachment; filename = ". $ file_name); // output file content @ readfile ($ file_type );}