Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件下载不工作_Php_File_Download - Fatal编程技术网

Php 文件下载不工作

Php 文件下载不工作,php,file,download,Php,File,Download,当我下载原始的zip时,它工作正常,但当我使用下面的标题和内容下载它时,它不工作。我知道最好采用这种方式,告诉浏览器如何处理文件,而不是让浏览器来处理,但我无法让它工作,所以我尝试使用header()转发 编辑: 抱歉,我的意思是它不起作用,文件以zip格式下载(全部9.3 MB),但我无法解压缩zip,因为它无效。尝试使用readfile()。中提供了一个示例 使用记事本或其他文本编辑器查看ZIP文件。检查前几行是否有PHP错误消息将文件搞糟。它可能是“headers ready sent”(

当我下载原始的zip时,它工作正常,但当我使用下面的标题和内容下载它时,它不工作。我知道最好采用这种方式,告诉浏览器如何处理文件,而不是让浏览器来处理,但我无法让它工作,所以我尝试使用header()转发

编辑:

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

尝试使用
readfile()
。中提供了一个示例


使用记事本或其他文本编辑器查看ZIP文件。检查前几行是否有PHP错误消息将文件搞糟。它可能是“headers ready sent”(标题已发送)消息或由于脚本处于安全模式而引发错误的
set\u time\u limit()
调用。

“它不工作”太含糊了。请解释一下怎么了。文件是否已损坏?请看一看:我使用的是相同的示例,但它不适用于我的
localhost
。我有
$file=“../files/myfile.pptx”
。这有什么问题吗?
            $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);
$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;
}