强制文件下载无效

时间:2013-06-14 19:24:03

标签: php html download

我试图在用户点击链接时下载该文件。该文件位于其他网站上,我正在传递文件的网址eg- xyz.com/filename.mp3。

echo "<a href=/direct_download.php?file=$link[$j]><img border='0' src='images/dl.gif' ></a>";

这是我的下载文件脚本。

<?php

$file_name = $_GET['file'];
//$files = rawurldecode($file_name);
echo $file_name;


if(is_file($file_name)) {
echo $file_name;

    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }


    switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   
    header('Expires: 0');       
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    
    header('Connection: close');
    readfile($file_name);       
    exit();

}
?>

2 个答案:

答案 0 :(得分:2)

像这样更改Content-Type

header('Content-Type: application/octet-stream');

编辑:虽然我仍然使用application/octet-stream作为内容类型,但请尝试一下:

<?php
/**
 * Make sure that the VERY FIRST THING IN THE DOCUMENT IS <?php
 * There can't be any spaces or anything else before it, because if
 * there is then header information has already started to be sent
 * back.
 */

/**
 * Avoid warnings, notices, etc by check $_GET's value first:
 */
$file_name = ($_GET && isset($_GET['file'])) ? $_GET['file'] : FALSE;
//$files = rawurldecode($file_name);

/**
 * As soon as you "echo" something, headers are sent back to the
 * browser, the same way adding space(s) before the opening <?php
 * tag will. Once headers are sent, they can't be modified or you
 * will receive an error.
 */
//echo $file_name;


if(is_file($file_name)) {
  /**
   * See above for info on headers.
   */
  //echo $file_name;

  if (ini_get('zlib.output_compression')) {
    ini_set('zlib.output_compression', 'Off');
  }
  switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
    case 'pdf': $mime = 'application/pdf'; break;
    case 'zip': $mime = 'application/zip'; break;
    case 'jpeg':
    case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
    /**
     * Although this works sometimes, I've had better luck with just
     * setting the content type to "application/octet-stream"
     * for anything that needs to be "forced" as a download.
     */
    // default: $mime = 'application/force-download';
    default: $mime = 'application/octet-stream';
  }
  header('Pragma: public');   
  header('Expires: 0');       
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime($file_name)).' GMT');
  header('Cache-Control: private',false);
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.filesize($file_name));    
  header('Connection: close');
  readfile($file_name);       
  exit();
}
?>

编辑(再次):

另外,请注意:

echo "<a href=/direct_download.php?file=$link[$j]><img border='0' src='images/dl.gif' ></a>";

几乎肯定会搞砸了。请尝试这样:

echo '<a href="/direct_download.php?file=' . $link[$j] . '"><img border="0" src="images/dl.gif" /></a>';

答案 1 :(得分:1)

我的猜测是你不包括目录。

$file_name = $_GET['file'];

$directory = 'set me up'; // directory for files

if(is_file($directory.$file_name)) {

$file_parts = pathinfo($directory.$file_name);

    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }


    switch($file_parts['extension']) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
        default: $mime = 'application/force-download';
    }

$size = filesize($directory.$filename) ;
header("Content-Type: application/force-download; name=\"". $file_parts['basename'] ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $file_parts['basename'] ."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($directory.$file_name));
}