2015-05-09 159 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