Php uses header () to download files. the downloaded files prompt that they are damaged and cannot be opened.

Source: Internet
Author: User
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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.