代码之家  ›  专栏  ›  技术社区  ›  Ben

文件下载不工作

  •  0
  • Ben  · 技术社区  · 15 年前

    当我下载原始的压缩文件时,它可以正常工作,但是当我使用下面的头文件下载时,它就不工作了。我知道走这条路线,告诉浏览器如何处理文件,而不是把它留给浏览器,这是更好的选择,但是我不能让它工作,所以我很想使用header()forward。

                $path = $this->tru->config->get('root.path').'/Digital Version of Book for Web.zip';
    
                set_time_limit(0);
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header('Content-Type: application/zip');
                header("Content-Transfer-Encoding: binary");
                header('Content-Disposition: attachment; filename="NewFileName.zip"');
                header('Content-Length: ' . filesize($path));
    
                $f = fopen($path, 'rb');
                fpassthru($f);
                fclose($f);
    

    编辑:

    抱歉,我的意思是它不起作用的是文件以zip格式(全部9.3MB)下载,但我无法解包zip,因为它无效。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Pekka    15 年前

    使用记事本或其他文本编辑器查看zip文件。检查前几行是否有PHP错误消息将文件搞砸。可能是“已发送的邮件头”或 set_time_limit() 由于脚本处于安全模式,调用引发错误。

        2
  •  0
  •   Thomas Müller    15 年前

    尝试使用 readfile() . 中提供了一个示例 PHP Manual .

    $file = 'monkey.gif';
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    
    推荐文章