2015-05-09 237 views
0

我的問題是,我的瀏覽器下載腳本將文件內容寫入瀏覽器,並且不會將其作爲文件從瀏覽器下載。PHP瀏覽器 - 文件下載

我的供應商是Strato。 (www.strato.de)

我使用這個PHP腳本下載文件:

if(file_exists($backup_file_path)) { 
      header('Content-Description: File Transfer'); 
      header('Content-Type: application/octet-stream'); 
      header('Content-Disposition: attachment; filename="'.basename($backup_file_path).'"'); 
      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($backup_file_path)); 
      ob_clean(); 
      flush(); 
      readfile($backup_file_path); 
      exit; 

      echo "<script>console.log('Download success');</script>"; 
     } 

在與XAMPP它正在本地主機,但在靈雲出現我所描述的問題。

請幫助我,如果你可以。 謝謝!

PS: PHP版本:5.5

回答

0

我看到這個......解決這個問題,但你的問題是另外一個...

改變這一點:

exit; 
echo "<script>console.log('Download success');</script>"; 

與此:

echo "<script>console.log('Download success');</script>"; 
exit; 

我建議要壓制這條線echo "<script>console.log('Download success');</script>";,可能干擾無線次下載文件...

爲了解決您的問題,您可能想試試這個...

$file = "test.zip"; 
$downloads_folder = "files/"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="' . $file . '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Connection: Keep-Alive'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder . $file))); 
$fh = fopen($downloads_folder . $file, "rb"); 
while (!feof($fh)) { 
    echo fgets($fh); 
    flush(); 
} 
fclose($fh); 
exit; 

讓我知道,如果你的作品,經過測試,對我的作品在Linux和Windows使用PHP 5.4和5.6

Download my file and test it

+0

謝謝您的回答! - 我試過你的版本,但沒有改變。與以前相同的問題 - 腳本將內容寫入瀏覽器,但不下載文件。 –

+1

我認爲你的問題出現在www.strato.de託管的'php.ini'中,爲了解決你的問題,你可能需要聯繫你的託管服務,並要求開一張協助票。如果腳本也適用於Windows,那麼唯一有罪的可能是Strato Hosting在他們的'php.ini'中存在一些問題。你可以在Windows上比較你的'php.ini'和Strato上的'php.ini'。爲此,創建一個名爲info.php的php文件並放入這行''?php phpinfo();然後上傳到您的Strato網絡空間,並將相同的文件放在Windows下的網絡空間中。看到'php.ini'的差異。 – Alessandro