2013-03-01 109 views
2

我創建了一個表單,當單擊一個包含的按鈕時,應該打開一個下載對話框來下載某個文件。該文件被放置在同一臺服務器上。PHP:點擊按鈕下載文件?

我想:

header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=" . $file . '"'); 

其中$文件是本地路徑+文件名,例如C:\ mypath中\ myfile.xls。這雖然不起作用。它提供了一個文件,但它不是一個有效的文件。我還能怎麼做?

注:我寫了c:\因爲它仍然在我的本地機器上進行測試。

謝謝!

回答

3

試試這個

header("Pragma: public", true); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Disposition: attachment; filename=".basename($file)); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($file)); 
die(file_get_contents($file)); 

我想的file_get_contents()函數不再用PHP 5.0.3

+0

是的!這似乎工作,但沒有嘗試其他人。 – user1856596 2013-03-01 11:39:28

0

PHP在服務器端運行,無法下載客戶端機器中的文件。

將文件上傳到服務器,然後提供該路徑進行下載。

+0

它仍用於測試的本地應用程序... – user1856596 2013-03-01 11:34:33

+0

把文件放在服務器的htdocs/yourfolder – 2013-03-01 11:35:11

+0

@ user1856596服務器不這麼認爲方式 – George 2013-03-01 11:35:26

0

路徑必須從站點根被refered ...移動文件 例如: 腳本路徑:C:/wamp/www/test.php 文件C:/script.js 則:

if(file_exists('../../user.js')) 
{ 
    echo "OK"; 
} 

仍然是一個壞IDEEA ..

1

工作試試這個:

 
$path = "http://www.example.com/files/"; 
$filename = "abc.gif"; 

header("Content-Type:image/gif"); 
header("Content-Disposition: attachment; filename=".$filename); 
header("Cache-control: private"); 
header('X-Sendfile: '.$path); 
readfile($path); 
exit;