Cite a case:
Copy code The code is as follows:
class Downfile {
function downserver ($file_name){
$file_path = "./img/".$file_name;
//Transcoding, the file name is converted to gb2312 to solve Chinese garbled characters
$file_name = iconv("utf-8" ,"gb2312",$file_name);
$file_path = iconv("utf-8","gb2312",$file_path);
$fp = fopen($file_path,"r") or exit(" The file does not exist");
//Define the variable size empty for each download
$buffer = 1024;
//Get the size of the file
$file_size = filesize($file_path);
//header("Content-type:text/html;charset=gb2312");
//Will write the four http protocol information used
header("Content-type:application/octet-stream ");
header("Accept-Ranges:bytes");//You can ignore it
header("Content-Length: ".$file_size);//The original text here is Accept-Length after checking the http protocol. This item
header("Content-Disposition:attachment;filename=".$file_name);
//Byte technology device, records the current number of bytes
$count = 0;
while (!feof($fp) && $file_size-$count>0){
//Read $buffer size data each time from the file stream opened by $fp
$file_data = fread($fp, $buffer);
$count+=$buffer;
//Read the read data
echo $file_data;
}
//Close the file stream
fclose ($fp);
}
}
?>
Call this function and pass in the file name to download the file, but be careful to modify $file_path
http://www.bkjia.com/PHPjc/779157.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779157.htmlTechArticleGive me an example: Copy the code as follows: ?php class Downfile { function downserver($file_name){ $file_path = "./img/".$file_name; //Transcoding, the file name is converted to gb2312 to solve Chinese confusion...