Home  >  Article  >  Backend Development  >  PHP file download example sharing_PHP tutorial

PHP file download example sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:08928browse

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

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn