【问题标题】:PHP issue with getting a redirect to work重定向工作的PHP问题
【发布时间】:2019-07-05 09:31:25
【问题描述】:

我正在尝试获取一个 zip 文件以自动下载:

            $attachment_location =  "https://dev.com/daily/updates/com_prog_v1_8_0.zip";

            if (file_exists($attachment_location)) {

                header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
                header("Cache-Control: public"); // needed for internet explorer
                header("Content-Type: application/zip");
                header("Content-Transfer-Encoding: Binary");
                header("Content-Length:".filesize($attachment_location));
                header("Content-Disposition: attachment; filename=com_prog_v1_8_0.zip");
                readfile($attachment_location);
                die(); 

            }

我尝试在浏览器中进行链接,文件下载没有问题。我不确定如何调试它。据我所知,这是对的。我期待运行这个文件(和 index.php 文件)并让它开始下载文件。我仔细看了其他帖子,但无法找出问题所在。

我也试过了

            file_put_contents("com_prog_v1_8_0.zip", fopen("https://dev.com/daily/updates/com_prog_v1_8_0.zip", 'r'));

但这也会引发

错误:找不到文件。

任何我能做些什么来追踪问题的想法都会很棒。我敢肯定它一定是我看不到的东西!

谢谢

【问题讨论】:

  • 你是如何重定向的?
  • 嗨我试过 curl 但它不起作用 - 同样的错误
  • 你是如何重定向的?
  • 我只是使用上面的代码来下载文件

标签: php redirect header location


【解决方案1】:

我不相信 file_exist() 可用于 url。试试这样的:

function url_exists($url){
    $ccmd = curl_init($url);    
    curl_setopt($ccmd, CURLOPT_NOBODY, true);
    curl_exec($ccmd);
    $rtncode = curl_getinfo($ccmd, CURLINFO_HTTP_CODE);
    curl_close($ccmd);

    if($rtncode == 200){
       return true;
    }
    return false;  
}

【讨论】:

  • 我取出if语句,页面报500错误
  • 您是否检查了错误日志以了解错误来自何处?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多