PHP with header () to implement file download, download the file hint is broken can not open the solution _php Tutorial

Source: Internet
Author: User
Tags fread
This site has previously shared a file download function, the specific article connection address is as follows:

PHP File Download function (code)

Recently a customer reflects that all the pictures on their website can not download, download the images are broken, direct results can not open, the author tested the next discovery does have this problem, carefully read the source code, found the root cause of the problem in fread this function, The second parameter of the Fread function is to set the maximum number of bytes to read, and the test finds that the maximum number of bytes that can be read by the Fread function is limited, only 8,192 bytes, or 8KB, for files that exceed this size, and if the file is to be fully read, it needs to be looped until the end of the file. Comprehensive above to make some corrections, the following code is after the rest of the code, after testing the problem solved.

function Download ($file _url, $new _name= ") {if (!isset ($file _url) | | Trim ($file _url) = = ") {return ' 500 ';} if (!file_exists ($file _url)) {//Check the file for the existence of return ' 404 ';} $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 label 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 contents @readfile ($file _type);}

Articles you may be interested in

    • Upload and download files using SECURECRT (with SZ and RZ commands)
    • Hosts file location and how to open Hosts file
    • PHP parsing file header information to determine the type of upload file
    • PHP Empty (delete) the file under the specified directory, do not delete the Directory folder method
    • PHP uses Curl Functions to crawl Web pages and download files in multiple threads
    • PHP File Download function (code)
    • PHP limit file Download speed function
    • Move the mouse to text on the floating Layer hint effect (compatible with IE, Firefox and other browsers)

http://www.bkjia.com/PHPjc/764096.html www.bkjia.com true http://www.bkjia.com/PHPjc/764096.html techarticle This site has previously shared a file download function, the specific article connection address is as follows: Php file download function (code) Recently a customer reflects all the pictures on their website can not download, ...

  • 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.