【问题标题】:Download file from server using php script. File can not be downloaded使用 php 脚本从服务器下载文件。文件无法下载
【发布时间】:2014-08-26 09:33:42
【问题描述】:

请帮帮我!

我正在尝试使用远程服务器上的脚本下载文件。脚本开始下载,但挂起(在 chrome 中显示“正在启动”并挂起)。(我在本地安装了 MAMP 并尝试从本地服务器下载文件,它工作正常。)

可能是php.ini文件配置错误?

我正在使用的脚本:

   <?php

   $url = "http://cs4-2v4.vk.me/p22/fbb2ec25fb8f67.mp3";

   header("Content-Disposition: attachment; filename=music.mp3");

   ob_flush();
   flush();

   $fp = fopen($url, "rb");
   while (!feof($fp))
   {
       print(fread($fp,8192));
       ob_flush();
       flush();
   }
   fclose($fp);
   ?>

谢谢!

【问题讨论】:

  • 您的脚本看起来很奇怪,原因有很多。当您使用绝对 url 重新分配 $_GET 时,为什么要从 $_GET 分配一个值?您不应该首先调用ob_start(),在读取整个文件之前也不需要使用ob_flush()?我没有看到任何错误检查,您能否验证文件是否正确打开?
  • 对不起。 “复制粘贴”后的错误
  • 谢谢!试过了,但没有任何改变。
  • 要像上面那样使用网址,您还应该确保在您的php.ini 中启用allow_url_fopen。由于代码在本地工作,但不能在远程服务器上工作,我猜这就是问题所在。
  • 谢谢!我会检查的。

标签: php


【解决方案1】:
$file_name = '1353.zip';
$file_url = 'http://download.krizna.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);

【讨论】:

    【解决方案2】:

    感谢所有回复的人。

    错误是此链接不存在该文件(file_exists($path) 不适用于 url)。在本地服务器上一切正常,因为请求来自相同的 IP 地址,用户已在社交网络 vk.com 中注册。

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2015-03-21
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多