【问题标题】:How to push Headers from a file on an external server to download?如何从外部服务器上的文件推送标题进行下载?
【发布时间】:2009-06-08 13:38:36
【问题描述】:

以下脚本是我通常用来将标头推送到浏览器的脚本,以便出现对话框供用户下载文件。

但是,在这种情况下,文件位于不同的服务器上。我虽然这应该没有什么区别,但是当我使用 externam MP3 文件的 URL 执行此脚本时,它会给我一个“错误:找不到文件”。但是,此文件存在,我可以使用传递给此脚本的相同 URL 来访问它。

任何想法为什么?我将不胜感激。

<?php session_start();

//below variable contains full path to external site where file resides
$filename = $_SESSION['$serverURL'].'audio/'.$_SESSION['fileName'].'.mp3';
//below variable contains a chosen filename of the file to be downloaded
$properFilename = $_GET['properFilename'].'.mp3';

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  //echo "download file NOT SPECIFIED";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  //echo "ERROR: File not found";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();   

?>

【问题讨论】:

    标签: php file download header


    【解决方案1】:

    单引号字符串不会被解析为变量,所以$_SESSION['$serverURL'] 可能不会像你期望的那样工作。我怀疑你的意思是$_SESSION[$serverURL]$_SESSION['serverURL']

    同时调用filesize()readfile() 可能会导致您的脚本发出两个HTTP 请求以从另一台服务器获取文件(除非它以某种方式被缓存)。您可以使用 cURL 在一个 HTTP 请求中执行此操作,这可能是一个更好的选择。这是一个简短的示例,您应该能够对其进行调整以做您想做的事情。您可能还需要考虑从其他服务器(如果可靠)转发其他标头,例如 Content-Type 标头,而不是自己重新生成它们。

    <?php
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://example.com');
    
    //set callbacks to receive headers and content
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'on_receive_header');
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'on_receive_content');
    
    //send your other custom headers somewhere like here
    
    if (false === curl_exec($ch)) {
        //handle error better than this.
        die(curl_error($ch));   
    }
    
    function on_receive_header($ch, $string) {
        //You could here forward the other headers received from your other server if you wanted
    
        //for now we only want Content-Length
        if (stripos($string, 'Content-Length') !== false) {
            header($string);   
        }
    
        //curl requires you to return the amount of data received
        $length = strlen($string);
        return $length;
    }
    
    function on_receive_content($ch, $string) {
        echo $string;
    
        //again return amount written
        $length = strlen($string);
        return $length;
    }
    

    【讨论】:

    • 感谢您的回复。我实际上在会话变量上犯了一个错误。应该是 $_SESSION['serverURL'].更正后,我仍然遇到同样的问题。我什至删除了发送文件大小的行。即使这样,它仍然没有工作!
    • 当我删除检查文件是否存在的条件时,它实际上工作了。我添加了行 ob_clean();冲洗();在 readfile() 之前;我将尝试实现您的 CURL 大小吸气剂。 :)
    猜你喜欢
    • 2012-02-15
    • 2010-09-15
    • 1970-01-01
    • 2021-07-29
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多