PHP with header () implementation file download, download the file hint is broken can not open the solution

Source: Internet
Author: User
Tags fread header php file return trim

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 their site all the pictures can not download, downloaded pictures are prompted to file is damaged, directly caused by the failure to open, the author tested the following found that there is really this problem, carefully looked at the source code, found the root of the problem in fread this function, The second parameter of the Fread function is to set the maximum number of bytes to read. The experiment found that the Fread function can read the maximum number of bytes is limited, only 8,192 bytes, that is, 8KB, for files larger than this size, if you want full read, you need to cycle read until the end of the file. Comprehensive above done some corrections, the following code is after the rest of the code after the test problem solved.

function Download ($file _url, $new _name= ') {
	if (!isset ($file _url) trim ($file _url) = = ') {return
		';
	}
	if (!file_exists ($file _url)) {//check if the file exists 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 Alixixi
	header ("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 that you may be interested in

    • Use SecureCRT to upload and download files (using the SZ and RZ commands)
    • Hosts file location and how to open the Hosts file
    • PHP empty (delete) files in the specified directory, do not delete the Directory folder method
    • PHP File Download function (code)
    • PHP Analysis file header information to determine the type of upload file
    • PHP uses Curl functions to implement multi-threaded crawl Web pages and download files
    • Mobile mouse to the text appears floating layer hint effect (compatible with IE, Firefox and other browsers)
    • PHP limits File Download speed function


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.