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