Php uses header () to download files. the downloaded files prompt is damaged and cannot be opened. _ PHP Tutorial

Source: Internet
Author: User
Php uses header () to download files. the downloaded files prompt that they are damaged and cannot be opened. This site has previously shared a file download function. the connection address of the specific article is as follows: PHP file download function (code). a recent customer reported that all the images on their website could not be downloaded, this site has previously shared a file download function. the connection address of the specific article is as follows:

Php file download function (code)

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 );}
Articles you may be interested in
  • Use SecureCRT to upload and download files (using sz and rz commands)
  • Hosts file location and how to open the hosts file
  • PHP analyzes the file header information to determine the type of the uploaded file
  • Php clears (deletes) the files in the specified directory, and does not delete the directory folder.
  • PHP uses Curl Functions to Capture webpages and download files with multiple threads
  • Php file download function (code)
  • Php's function of limiting file download speeds
  • Move the mouse over the text to display the floating layer prompt special effects (compatible with all browsers such as IE and Firefox)

Php file download function (code) A recent client reported that all images on their website could not be downloaded ,...

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.