Php 如何将文件头从外部服务器上推送到下载?

Php 如何将文件头从外部服务器上推送到下载?,php,file,download,header,Php,File,Download,Header,下面的脚本是我通常用于将标题推送到浏览器的脚本,以便用户下载文件时出现对话框 但是,在这种情况下,文件驻留在不同的服务器上。我认为这应该没有什么区别,但当我用externammp3文件的URL执行这个脚本时,它会给我一个“错误:找不到文件”。但是,这个文件存在,我可以使用传递给这个脚本的相同URL访问它 你知道为什么吗?我将感谢任何帮助 <?php session_start(); //below variable contains full path to external site

下面的脚本是我通常用于将标题推送到浏览器的脚本,以便用户下载文件时出现对话框

但是,在这种情况下,文件驻留在不同的服务器上。我认为这应该没有什么区别,但当我用externammp3文件的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();   

?>

单引号字符串不会被解析为变量,因此
$\u会话['$serverURL']
可能无法按预期工作。我猜你指的是
$\u会话[$serverURL]
$\u会话['serverURL']

同时调用
filesize()
然后调用
readfile()
可能会导致脚本发出两个HTTP请求,从另一台服务器获取文件(除非以某种方式缓存)。您可以使用cURL在一个HTTP请求中完成这项工作,这可能是一个更好的选择。下面是一个简单的例子,你应该能够调整它来做你想做的事情。您可能还想考虑从其他服务器(如果可靠)转发其他头文件,例如内容类型头,而不是自己重新生成。
<?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;
}

谢谢您的回复。实际上,我在会话变量上犯了一个错误。应该是$\u会话['serverURL']。在纠正这个问题之后,我仍然有同样的问题。我甚至删除了发送文件大小的行。即使那样,它仍然不起作用!当我删除检查文件是否存在的条件时,它实际上起了作用。我添加了行ob_clean();冲洗();在readfile()之前;我将尝试实现卷曲大小getter.:)