php實現(xiàn)簡單文件下載的方法
介紹的php文件下載代碼,只是簡單實現(xiàn)了一張圖片的下載功能,還不完善,最好是封裝到一個類里,或是采用函數(shù)調(diào)用。感興趣的朋友可以在此基礎(chǔ)上加以完善!
php文件下載代碼如下:
<?php
$file_name = "2.jpg";/pic/p>
define("SPATH","/php/image/");/pic/p>
$file_sub_path = $_SERVER['DOCUMENT_ROOT'];/pic/p>
$file_path = $file_sub_path.SPATH.$file_name;/pic/p>
/pic/p>
if(!file_exists($file_path)){
echo "該文件不存在";
return;
}
$fp = fopen($file_path,"r");/pic/p>
$file_size = filesize($file_path);/pic/p>
/*
*下載文件需要用到的header
*/
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;
/pic/p>
while(!feof($fp) && $file_count<$file_size){
$file_con = fread($fp,$buffer);
$file_count += $buffer;
echo $file_con;/pic/p>
}
fclose($fp);
?>
【php實現(xiàn)簡單文件下載的方法】相關(guān)文章:
PHP中讀取大文件實現(xiàn)方法10-09
PHP下載保存文件保存到本地的方法02-16
php實現(xiàn)通過ftp上傳文件06-28
PHP文件鎖與進程鎖的實現(xiàn)12-12
jQuery Mobile + PHP實現(xiàn)文件上傳03-20
- 相關(guān)推薦