PHP下载服务器上的文件,能够经由过程自定义函数要领downtemplateAction()来完成下载,在其要领体内有推断文件是不是存在以及是不是胜利下载、封闭文件等功能。
相干引荐:《php教程》
细致示例以下:
/** * @todo 下载文件 */ public function downtemplateAction(){ header("Content-type:text/html;charset=utf-8"); $file_name = "template.xlsx"; $file_name = iconv("utf-8","gb2312",$file_name); $file_sub_path = APP_PATH.'/data/obj/2018-03-21/'; $file_path=$file_sub_path.$file_name; if(!file_exists($file_path)) { echo "下载文件不存在!"; exit; } $fp=fopen($file_path,"r"); $file_size=filesize($file_path); //下载文件须要用到的头 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:".$file_size); Header("Content-Disposition: attachment; filename=".$file_name); $buffer=1024; $file_count=0; while(!feof($fp) && $file_count<$file_size) { $file_con=fread($fp,$buffer); $file_count+=$buffer; echo $file_con; } fclose($fp); //封闭这个翻开的文件 }
以上就是php怎样从服务器下载文件的细致内容,更多请关注ki4网别的相干文章!