强制下载PHP - 不下载整个文件

时间:2012-06-11 12:49:05

标签: php download

我在强制下载某些mp3 / zip文件时遇到问题。

问题是有些用户体验到整个文件没有下载,我无法弄清楚原因..

if(is_file($file_name))
{

    /*
        Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
        3.  Check user permissions
        4.  Anything you want!
    */

    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    switch(strtolower(substr(strrchr($file_name,'.'),1)))
    {
        case 'mp3': $mime = 'audio/mpeg'; break;
        case 'zip': $mime = 'application/zip'; break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    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));    // provide file size
    header('Connection: close');
    readfile($file_name);       // push it out
    exit();

}

这是我的代码..

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

感谢您的所有帮助,

但是我发现了我的问题的另一个解决方案,并使用了mod_xsendfile。它已经完成了我的工作:)

相关问题