PHP下載保存文件保存到本地的方法
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();
?>
-
PHP將XML轉為數組的方法
本文詳細介紹了,在php編程中,將XML數據轉換為數組的詳細過程,剖析了xml轉為php數組的具體方法,感興趣的朋友可以參考學習下。PHPXML轉數組首先,得到一個xml型的對象:複製代碼代碼示例:$resp=$this->c->execute($req,$sessionKey);//獲得xml對象$items=$resp->items;...
-
用PHP實現文件上傳二法
PHP(HypertextPreprocessor)是一種HTML內嵌式的語言(類似IIS上的ASP)。而PHP獨特的語法混合了C、Java、Perl以及PHP式的新語法。它可以比CGI或者Perl更快速的執行動態網頁。除此之外,用PHP寫出來的Web後端CGI程序,可以很輕易的移植到不同的系統平台上。我們在做...
-
PHP如何使用curl發送GET和POST請求
導語:PHP如何使用curl發送GET和POST請求,具體詳情請閲讀下面代碼,更多詳情請關注應屆畢業生考試網。使用CURL發送請求的基本流程使用CURL的PHP擴展完成一個HTTP請求的發送一般有以下幾個步驟:1.初始化連接句柄;2.設置CURL選項;3.執行並獲取結果;4.釋放VURL連接句柄。...
-
PHP常用開發技巧
PHP開發常用技巧能使你在開發過程中快而有效.以下就是小編精心推薦PHP常用開發技巧,希望對大家有幫助!1使用dowhile避免多層if語句嵌套我們直接舉例説明:實現方式①if($name=='hedong'){if($sex=='male'){if($major=='PHP'){$ret='...