- 相關推薦
PHP下載保存文件保存到本地的方法
PHP原始為Personal Home Page的縮寫,已經正式更名為 "PHP: Hypertext Preprocessor"。注意不是“Hypertext Preprocessor”的縮寫,這種將名稱放到定義中的寫法被稱作遞歸縮寫,以下是小編為大家搜索整理的PHP下載保存文件保存到本地的方法,歡迎大家閱讀!更多精彩內容請及時關注我們應屆畢業生考試網!
第一種:
function downfile()
{
$filename=realpath("resume.html"); //文件名
$date=date("Ymd-H:i:m");
Header( "Content-type: application/octet-stream ");
Header( "Accept-Ranges: bytes ");
Header( "Accept-Length: " .filesize($filename));
header( "Content-Disposition: attachment; filename= {$date}.doc");
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}.doc");
$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= 4.doc");
$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='.basename($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: '.filesize($filename));
readfile($thefile);
exit();
?>
更多相關文章推薦:
3.解決PHP的failed opening required問題的方法
6.php采用ajax數據提交post與post常見方法總結
【PHP下載保存文件保存到本地的方法】相關文章:
PHP遍歷目錄文件常用方法03-24
PHP中讀取大文件實現方法詳解11-30
php是什么文件03-30
PHP文件怎么操作11-26
php查找指定目錄下指定大小文件的方法03-02
php文件鎖怎么用03-27
PHP常用的文件操作函數11-26
php下載代碼怎么寫11-18
PHP獲取遠程文件大小的3種解決方法03-30