Copy Code code as follows:
/*======================================================
$FileName for file name, must pass
$FilePath is the file path. Optional, can be a relative or absolute path
Paths can only be made up of English and data, not with Chinese
======================================================*/
<?php
Header ("Content-type:text/html;charset=utf-8");
if (strlen ($FileName) <=3) {echo download failed: You so downloaded the file information incorrectly "; return;}
$FileName =iconv ("Utf-8", "gb2312", $FileName);//File name format conversion to prevent Chinese garbled
Start to judge the path
if (!is_null ($FilePath) &&strlen ($FilePath) >1) {
if (substr ($FilePath, 0, 1) = = '/') {//To determine whether it is an absolute path
$FilePath =$_server[' Document_root ']. $FilePath;
}
if (substr ($FilePath,-1)!= "/") {//check last for/end
$FilePath = $FilePath. ' /';
}
if (Is_numeric ($FilePath, ": \")) {//Check for absolute path (STRPOS)
$FilePath =str_replace ("/", "\", $FilePath);
}
}elseif (strlen ($FilePath) ==1&& $FilePath!= "/") {
$FilePath = $FilePath. " /";
}else{
$FilePath = "";
}
if (!file_exists ($FilePath. $FileName)) {
echo "Download failed: The file you want to download is not found";
}
/*================================================
Send a 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);//Here The client's pop-up dialog box, the corresponding filename
/*================================================
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);
}
?>