PHP原始為Personal Home Page的縮寫,已經正式更名為 "PHP: Hypertext Preprocessor"。注意不是“Hypertext Preprocessor”的縮寫,這種將名稱放到定義中的寫法被稱作遞迴縮寫,以下是小編為大家搜尋整理的PHP下載儲存檔案儲存到本地的方法,歡迎大家閱讀!更多精彩內容請及時關注我們應屆畢業生考試網!
第一種:
function downfile()
{
$filename=realpath(""); //檔名
$date=date("Ymd-H:i:m");
Header( "Content-type: application/octet-stream ");
Header( "Accept-Ranges: bytes ");
Header( "Accept-Length: " size($filename));
header( "Content-Disposition: attachment; filename= {$date}");
echo file_get_contents($filename);
readfile($filename);
}
downfile();
?>
或
?
function downfile($fileurl)
{
ob_start();
$filename=$fileurl;
$date=date("Ymd-H:i:m");
header( "Content-type: application/octet-stream ");
header( "Accept-Ranges: bytes ");
header( "Content-Disposition: attachment; filename= {$date}");
$size=readfile($filename);
header( "Accept-Length: " .$size);
}
$url="url地址";
downfile($url);
?>
第二種:
function downfile($fileurl)
{
$filename=$fileurl;
$file = fopen($filename, "rb");
Header( "Content-type: application/octet-stream ");
Header( "Accept-Ranges: bytes ");
Header( "Content-Disposition: attachment; filename= ");
$contents = "";
while (!feof($file)) {
$contents .= fread($file, 8192);
}
echo $contents;
fclose($file);
}
$url="url地址";
downfile($url);
?>
PHP實現下載檔案的兩種方法。分享下,有用到的朋友看看哦。
方法一:
?
/**
* 下載檔案
* header函式
*
*/
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='name($filepath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0′);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);
?>
瞭解php中header函式的用法。
方法二:
?
//檔案下載
//readfile
$fileinfo = pathinfo($filename);
header('Content-type: application/x-'.$fileinfo['extension']);
header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
header('Content-Length: 'size($filename));
readfile($thefile);
exit();
?>