作者guancheng (Austin)
看板PHP
標題Re: [請益] 有關目錄讀取問題??
時間Sun Jan 18 15:16:56 2009
我猜download.php裡應該就是類似readfile($_GET['file']);
先不說你的bug,他可能還會有些問題
1. php讀檔是依檔案系統的路徑,跟apache的虛擬目錄應該沒關係吧
2. 這樣傳絕對路徑去讀檔太危險了喔@@
$_GET['file']原本是傳絕對路改傳相對路徑
例如:
http://127.0.0.1/download.php?file=test.txt
程式碼你可以改成如下:
<?php
$path = 'd:/download/';
$file = $path.$_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;
} else {
echo 'File not exist!';
}
?>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.240.2.13
推 joedenkidd :謝謝好心人,我會試看看,不過我有一個疑問,這是不 01/18 20:55
→ joedenkidd :是針對檔案都在download下,如果download下還有其他 01/18 20:56
→ joedenkidd :目錄是否也能試用呢?我會先try看看!到時候再把心得 01/18 20:57
→ joedenkidd :放上來! 01/18 20:57
推 joedenkidd :我試過這個方法了,這是方法應該ok,謝謝! 01/18 23:09