2012-04-03 37 views
0

我有下面的腳本,用於使用PHP從文件夾下載文件,它在本地工作,但在線正在讀取並打印在頁面下載狀態。PHP正在讀取文件而不是下載

if($result && count($result) > 0){ 
    $row = $result[0]; 
    $book = true; 
    $Location = './files/'; 
    $bookLocation = $Location . $row['file_name']; // exampe: report.zip 
    if(file_exists($bookLocation)){ 
     $fileLocation = $bookLocation; 
     $file_size = filesize($fileLocation); 
     echo 'Please wait downloading the file....'; 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/force-download'); 
     header('Content-Disposition: attachment; filename=' . $row['file_name']); 
     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: ' . $file_size); 
     ob_clean(); 
     flush(); 
     readfile($fileLocation); 
     exit;  
    }else{ 
     echo '<h3>Sorry!, file not found...</h3>'; 
    } 
} 

的.htaccess腳本

RewriteEngine on 

RewriteRule ^$ index.html 
RewriteRule ^index.html$ index.php 

RewriteRule ^([0-9]{1,9})(/)([^/\.]+[^/\.]+)(_+[^/\.]+)(\.html|)/?$ index.php?id=$1&cat=$3 
RewriteRule ^([^/\.]+[^/\.]+)(\.html|)/?$ index.php?page=$1 
RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.html|)/?$ index.php?view_file=$3&file=$5 
RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.htm)/?$ index.php?download=$3 

RewriteRule ^(author)(/)(.*)(.html)/?$ index.php?user=$1 

感謝您的幫助。

+0

你不能從瀏覽器中運行它取代了線

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

? – zod 2012-04-03 14:43:16

+0

嘗試使用鏈接創建一個新文件..鏈接將到這個文件.. ..然後嘗試執行新的頁面並點擊鏈接! – zod 2012-04-03 14:45:36

+0

服務器最終是否修改了頭文件? – evotopid 2012-04-03 14:47:15

回答

2

在某些瀏覽器中,可能會將其設置爲自動下載並運行該特定文件類型。

如果是這樣,就沒有辦法「修復」這一點。瀏覽器決定如何處理文件,即使您告訴它下載文件,用戶可能會告訴它直接運行。

大編輯

雖然我先前的評論是正確的,你必須在你的代碼的一個重大問題:

$file_size = filesize($fileLocation); 
    echo 'Please wait downloading the file....'; 
    header('Content-Description: File Transfer'); 

必須刪除echo聲明。這肯定會導致問題,例如頭文件未被髮送。考慮到你的描述,我會說這是你的問題。

看到,一旦你發送任何內容,你不能發送更多的頭。

+0

好吧,它應該工作**如果**有一個'ob_start()'在某個地方,對嗎? – bububaba 2012-04-03 14:52:59

+0

回聲可能是問題,而不是任何其他問題,因爲他使用二進制作爲內容類型。 – Tei 2012-04-03 14:53:34

+0

我刪除了回聲,但仍然不能正常工作...... :( – 2012-04-03 17:33:30

0

如果已啓用fopen wrappers,則可以使用URL作爲具有此功能的文件名。有關如何指定文件名的更多詳細信息,請參閱fopen()。請參閱Supported Protocols and Wrappers以獲取有關各種包裝具有哪些功能的信息的鏈接,關於它們的用法的註釋以及它們可能提供的任何預定義變量的信息。

我希望這可以幫助,我發現它在here

+0

這就是我得到的結果:「sAÛ¿31」「ù\tºhëT VK/p <ÛºòÇ*ñœoŽ08úÍúœ+} \ ZZ:ÒšïU†_²iô©ËêiCŸo;μ? QðÕÁMìËg£Ð®üO³ìó]ĺ¡¥™Ø3å...W¤òçCLŒßFád$œTU¼s\ 「@ JI_ UUO] AG」 §'uI/ÊéphéyŠÎ* \ 「<啊·奧爾2Œ〜|îmƒäâ¤YlXâèSes€」 DD 〜œcæíœ,AAEI ARY/\t「cÁ.v )œÉæ¹U¸ 「我 ʬP」 W ... O÷Z *⨤â'l}¶¢ 2012-04-03 15:19:54

+0

嘗試不的.htaccess運行腳本 – 2012-04-04 07:57:41

0

基督教的建議,你必須刪除echo語句在你的代碼。如果它不適合你,即使以後的工作,嘗試用

header('Content-Type: application/octet-stream'); 
當地
相關問題