2012-12-08 56 views
2

在我的web服務器上,我有一個蝙蝠(無害)文件。html/php用戶下載文件

我有代碼,

<a href="test.bat" title="Bat">Test Bat File</a> 

但是,當用戶點擊,它顯示的代碼,而不是下載文件。
您必須右鍵單擊「另存爲...」下載bat文件。

用戶點擊時有沒有辦法下載(不必右鍵點擊另存爲)?
也許會彈出一個窗口詢問用戶他/她是否要下載該文件?

+1

http://www.finalwebsites.com/forums/topic/php-file-download – ethrbunny

回答

4

你可以編寫一個php文件,它添加一個content-disposition頭文件,將MIME類型設置爲二進制文件並回顯文件內容。

實施例:

file.php

$batchfile = file_get_contents('batchlocation'); 
$size = strlen($batchfile); 
header('Content-Disposition: attachment; filename="downloaded.bat"'); 
header('Content-Type: BAT MIME TYPE or something like application/octet-stream'); 
header('Content-Lenght: '.$size); 
echo $batchfile; 
+0

工作完美 –