PHP中的白痴证明,跨浏览器强制下载

Ada*_*iss 6 php header force-download

我正在使用强制下载主要下载网站上的拉链和mp3(http://pr1pad.kissyour.net) - 跟踪谷歌分析,数据库中的下载并隐藏真正的下载路径:

就是这样:

extending CI model

... - bunch of code

function _fullread ($sd, $len) {
 $ret = '';
 $read = 0;
 while ($read < $len && ($buf = fread($sd, $len - $read))) {
  $read += strlen($buf);
  $ret .= $buf;
 }
 return $ret;
}

function download(){    
    /* DOWNLOAD ITSELF */

    ini_set('memory_limit', '160M');
    apache_setenv('no-gzip', '1');
    ob_end_flush();

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public",FALSE);
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
     if (isset($_SERVER['HTTP_USER_AGENT']) && 
      (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
      header('Content-Type: application/force-download'); //IE HEADER
    header("Accept-Ranges: bytes");
    header("Content-Disposition: attachment; filename=\"" . basename("dir-with-    files/".$filename) . "\";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize("dir-with-files/".$filename));

    // Send file for download
    if ($stream = fopen("dir-with-files/$filename", 'rb')){
     while(!feof($stream) && connection_status() == 0){
      //reset time limit for big files
      set_time_limit(0);
      print($this->_fullread($stream,1024*16));
      flush();
     }
     fclose($stream);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是在LAMP上使用CI 1.7.2 - 这是我自己的方法,从各种互联网上的各种方法放在一起,因为在开发期间,出现了这些问题: - 服务器限制.ini_set没有帮助,所以我使用缓冲_fullread而不是正常fread,这是使用@readonly - ob_end_flush(),因为网站在CI1.7.2中做了,我需要清理缓冲区

现在......它不起作用.它确实,然后它停止显示预期的大小/下载时间 - 我试图清理它,当我清理代码时,发生了一些事情,我不知道什么和任何以前的版本 - 它没有工作(没有无论如何改变设置) - 编辑:不工作=将所有内容输出到浏览器窗口.

所以我说,拧紧它,我会看到这里.

所以,我基本上寻找脚本或函数,我可以把它放到我的输出模型,并将做:

  • 呼叫强制下载(在Chrome中开始下载,在IE,FF,Safari中打开模态打开/保存/取消)
  • 显示文件大小和估计的dl时间(这取决于浏览器,我知道,但首先,浏览器必须知道文件大小
  • IE6,7,8,FF3,Opera,Chrome&和PC + Mac(Linux ......我真的不在乎)的工作(测试和确认!) - 这是标题部分
  • 在服务器上,我也有类似56MB的内存限制,我无法添加,所以这也很重要

先感谢您.

编辑:现在我感觉更加紧张,从那时起/之前,因为我试图强制下载.htaccess - 虽然它有效,但它有很少的小/主要(选择你的)问题

  • 它显示了完整的路径(对我来说很小)
  • 它等待整个下载完成(显示为"连接"),然后只显示它的下载 - 并在一秒内下载(主要为我)

现在,虽然我删除了.htaccess,但它仍然等待下载完成(就像它首先下载到缓存一样)并且它只是得到connected并显示打开/保存对话框.

Ada*_*iss 8

所以,我使用了这个代码(它是在互联网上找到的可恢复的http下载的修改版本)

function _output_file($file, $path)
{
    $size = filesize($path.$file);

    @ob_end_clean(); //turn off output buffering to decrease cpu usage

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

    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');

    /* The three lines below basically make the 
    download non-cacheable */
    header("Cache-control: no-cache, pre-check=0, post-check=0");
    header("Cache-control: private");
    header('Pragma: private');
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    // multipart-download and download resuming support
    if(isset($_SERVER['HTTP_RANGE']))
    {
        list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
        list($range) = explode(",",$range,2);
        list($range, $range_end) = explode("-", $range);
        $range=intval($range);
        if(!$range_end) {
            $range_end=$size-1;
        } else {
            $range_end=intval($range_end);
        }

        $new_length = $range_end-$range+1;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $range-$range_end/$size");
    } else {
        $new_length=$size;
        header("Content-Length: ".$size);
    }

    /* output the file itself */
    $chunksize = 1*(1024*1024); //you may want to change this
    $bytes_send = 0;
    if ($file = fopen($path.$file, 'rb'))
    {
        if(isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);

        while
            (!feof($file) && 
             (!connection_aborted()) && 
             ($bytes_send<$new_length) )
        {
            $buffer = fread($file, $chunksize);
            print($buffer); //echo($buffer); // is also possible
            flush();
            $bytes_send += strlen($buffer);
        }
    fclose($file);
    } else die('Error - can not open file.');

die();
}
Run Code Online (Sandbox Code Playgroud)

然后在模型中:

function download_file($filename){
    /*
        DOWNLOAD
    */
    $path = "datadirwithmyfiles/"; //directory

    //track analytics

    include('includes/Galvanize.php'); //great plugin
    $GA = new Galvanize('UA-XXXXXXX-7');
    $GA->trackPageView();

    $this->_output_file($filename, $path);

}
Run Code Online (Sandbox Code Playgroud)

它在Win/MAC上提到的所有浏览器中都能正常工作 - 到目前为止,没有问题.