phpダウンロード保存ファイルをローカルに保存する2つの方法

6782 ワード

phpダウンロード保存ファイルをローカルに保存する2つの方法.
1つ目:
 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);

?>

2つ目:
 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 ?>