<?PHP//Demo Download a picture $file _name= "Sunset.jpg"; $file _name=Iconv("Utf-8", "gb2312",$file _name);//the file name is processed because PHP is not friendly with Chinese support "Turn Utf-8 into gb2312"//1. Open the file to determine if there is if(!file_exists($file _name)){ Echo"File does not exist"; return ; } $fp=fopen($file _name, "R");//open a file in read-only mode $file _size=filesize($file _name); Echo"File Size:".filesize($file _name); //input File Label Header("Content-type:application/octet-stream"); Header("Accept-ranges:bytes"); Header("Accept-length:".)$file _size); Header("Content-disposition:attachment; Filename= ".$file _name); //loopback data to the client $buffer= 1024;//define the size of the data to be read//In order to download security, you can do a file byte read Word converter $file _count=0; while(!feof($fp) && ($file _size-$file _count>0))//feof to determine if a file is closed { $file _data=fread($fp,$buffer);//reading Data $file _count+=$buffer;//statistics Read how many bytes//send some data back to the browser Echo $file _data; } //Close File fclose($fp);?>
PHP Learning Note 38 "Download"