This file download instance is very detailed he is combining the header function with the while Fread function to read out the file and then send to the client, a standard file download instance.
A small instance of a php file download
/*======================================================
$FileName is the file name, must pass
The $FilePath is the file path. Optional, can be a relative path or an absolute path
The path can only be composed of English and data, not with Chinese
If you have any questions, please contact the blogger to indicate
======================================================*/
The code is as follows |
Copy Code |
Header ("Content-type:text/html;charset=utf-8"); if (strlen ($FileName) <=3) {echo "Download failed: You downloaded the file information incorrectly"; return;} $FileName =iconv ("Utf-8", "gb2312", $FileName);//File name format conversion in case of Chinese garbled Start judging the path if (!is_null ($FilePath) &&strlen ($FilePath) >1) { if (substr ($FilePath, 0, 1) = = '/') {//Determine if absolute path $FilePath =$_server[' Document_root '). $FilePath; } if (substr ($FilePath,-1)! = "/") {//check if last is/end $FilePath = $FilePath. ' /'; } if (Is_numeric (Strpos ($FilePath, ": \"))) {//Check for absolute path $FilePath =str_replace ("/", "\", $FilePath); } }elseif (strlen ($FilePath) ==1&& $FilePath! = "/") { $FilePath = $FilePath. " /"; }else{ $FilePath = ""; } if (!file_exists ($FilePath. $FileName)) { echo "Download failed: The file to be downloaded was not found"; } /*================================================ Send download-Related header information =================================================*/ Header ("Content-type:application/octet-stream"); Header ("Accept-ranges:bytes");//return by byte size Header ("Accept-length: $FileSize");//Return file size Header ("content-disposition:attachment; Filename= ". $FileName);//The Client's popup dialog box, corresponding to the file name /*================================================ Start Download related =================================================*/ $FileSize =filesize ($FilePath. $FileName);
$File =fopen ($FilePath. $FileName, "R");//Open File
$FileBuff = 512;
while ($FileSize >=0) {
$FileSize-= $FileBuff;
Echo fread ($File, $FileBuff);
}
Fclose ($File); } ?> |
Summarize
This download example and support the Chinese dissemble, the file at the beginning of the UFT8 encoding conversion.
http://www.bkjia.com/PHPjc/633123.html www.bkjia.com true http://www.bkjia.com/PHPjc/633123.html techarticle This file download instance is very detailed he is combining the header function with the while Fread function to read out the file and then send to the client, it is a standard file download real ...