php 下载保存文件保存到本地的两种方法

php下载保存文件保存到本地的两种方法。

第一种:

 1 <?php 
 2 function downfile()
 3 {
 4 $filename=realpath("resume.html"); //文件名
 5 $date=date("Ymd-H:i:m");
 6 Header( "Content-type: application/octet-stream "); 
 7 Header( "Accept-Ranges: bytes "); 
 8 Header( "Accept-Length: " .filesize($filename));
 9 header( "Content-Disposition: attachment; filename= {$date}.doc"); 
10 echo file_get_contents($filename);
11 readfile($filename); 
12 }
13 downfile();
14 ?>

<?php 
//下载文件保存到本地
//www.jbxue.com
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);
?>

第二种:

 1 <?php 
 2 //下载文件保存至本地
 3 //www.jbxue.com
 4 function downfile($fileurl)
 5 {
 6 $filename=$fileurl;
 7 $file = fopen($filename, "rb"); 
 8 Header( "Content-type: application/octet-stream "); 
 9 Header( "Accept-Ranges: bytes "); 
10 Header( "Content-Disposition: attachment; filename= 4.doc"); 
11 $contents = "";
12 while (!feof($file)) {
13 $contents .= fread($file, 8192);
14 }
15 echo $contents;
16 fclose($file); 
17 }
18 $url="url地址";
19 downfile($url);
20 ?>