首頁  >  文章  >  後端開發  >  php 下載儲存檔案儲存到本機的兩種實作方法

php 下載儲存檔案儲存到本機的兩種實作方法

高洛峰
高洛峰原創
2017-01-06 14:19:273978瀏覽

第一種:

<?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();
?>

   

<?php
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);
?>

   

分享下,有用到的朋友看看哦。

方法一:

<?php
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函數的用法。

方法二:

<?php
/**
* 下载文件
* header函数
*
*/
header(&#39;Content-Description: File Transfer&#39;);
 
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename=&#39;.basename($filepath));
header(&#39;Content-Transfer-Encoding: binary&#39;);
header(&#39;Expires: 0′);
header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(&#39;Pragma: public&#39;);
header(&#39;Content-Length: &#39; . filesize($filepath));
readfile($file_path);
?>

   

更多php 下載保存檔案儲存到本地的兩種實作方法相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn