2009年1月17日 星期六

檔案下載,詢問你是否要下載檔案

參考網站:http://tw.php.net/header
     http://blog.roodo.com/jaceju/archives/805389.html

<?PHP
$file_path = "/localhost/downlaodfile.txt";

//把資料寫進檔案
fwrite($fp, "testtesttestetest");

//開啟檔案 =>w 把檔案開啟成寫入模式,檔案裡既存內容將全部遺失;若檔案不存在,PHP會建立它
if (!$fp = fopen($file_path, "w")){
 echo "開啟檔案失敗";
 exit;
}

//檔案大小
$file_size = filesize($file_path);

// fix for IE catching or PHP bug issue
header('Pragma: public');

// set expiration time
header('Expires: 0');

header('Last-Modified: ' . gmdate('D, d M Y H:i ') . ' GMT');

// browser must download file from server instead of cache HTTP/1.1
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Cache-Control: private', false);

// force download dialog
header('Content-Type: application/octet-stream');

header('Content-Length: ' . $file_size);

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
// It will be called download.txt
header('Content-Disposition: attachment; filename="download.txt";');
header('Content-Transfer-Encoding: binary');

readfile($file_path); //開啟檔案
fclose($fp); //關閉檔案
?>

沒有留言: