Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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关闭zipArchive打印垃圾文本(数据转储)_Php_Http Headers_Ziparchive - Fatal编程技术网

PHP关闭zipArchive打印垃圾文本(数据转储)

PHP关闭zipArchive打印垃圾文本(数据转储),php,http-headers,ziparchive,Php,Http Headers,Ziparchive,我一直在努力学习如何在php中正确使用zipArchive函数来创建和下载包含选定文件的zip文件夹 在创建zip文件并将其存储在服务器上之后,它可以正常工作,但在$zip->close()之后,我无法让下载自动启动;命令它打印一页又一页的垃圾字符,就像打印zip文件一样。下面是其中的一部分: 我从这里得到了我的基本代码: 下面是我的代码中的外观: function zipFilesAndDownload($file_names,$archive_file_name,$file_path)

我一直在努力学习如何在php中正确使用zipArchive函数来创建和下载包含选定文件的zip文件夹

在创建zip文件并将其存储在服务器上之后,它可以正常工作,但在$zip->close()之后,我无法让下载自动启动;命令它打印一页又一页的垃圾字符,就像打印zip文件一样。下面是其中的一部分:

我从这里得到了我的基本代码:

下面是我的代码中的外观:

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
    {
        //create the object
        $zip = new ZipArchive();
        //create the file and throw the error if unsuccessful
        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==TRUE)
        {
            exit("cannot open <$archive_file_name>\n");
        }

        //add each files of $file_name array to archive
        foreach($file_names as $files)
        {
            $zip->addFile($file_path.$files,$files);
        }
        $zip->close();

        //THIS IS WHEN THE JUNK TEXT STARTS

        //then send the headers to force download the zip file
        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=".$archive_file_name);
        header("Pragma: no-cache");
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
        readfile($archive_file_name);
        exit;
    }
函数zipFilesAndDownload($file\u name、$archive\u file\u name、$file\u path)
{
//创建对象
$zip=新的ZipArchive();
//创建文件并在失败时抛出错误
如果($zip->open($archive\u file\u name,ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)!==TRUE)
{
退出(“无法打开”);
}
//将$file\u name数组中的每个文件添加到存档
foreach($file\u名称为$files)
{
$zip->addFile($file\u path.$files,$files);
}
$zip->close();
//这是垃圾文本开始的时候
//然后发送标题以强制下载zip文件
标题(“内容类型:应用程序/zip”);
标题(“内容处置:附件;文件名=。$archive\u file\u name”);
标题(“杂注:无缓存”);
标题(“到期日期:1997年7月26日星期六05:00:00 GMT”);
readfile($archive\u file\u name);
出口
}
有人知道是什么导致了垃圾短信吗?我是否错误地使用了标题?如有必要,我可以提供进一步的代码,但我相当肯定变量过程是可靠的。我已经在Chrome上进行了测试,但我已经证实这种行为在IE和Firefox中都存在

[更新]我发现如果我在当前目录的子文件夹中创建zip文件(而不是直接在当前目录中),它不会输出页面上的所有垃圾文本


现在我的问题更多的是,我希望从标题中获得的自动下载功能不起作用,而且从来没有起过作用。任何我应该寻找的错误或事件,可能会揭示出什么是错误的?

似乎是标题有问题。显示的有线垃圾只是zip文件中的原始数据。请检查$archive\u file\u name是否正确。尝试更改标题,如下所示:

// It should contain only the filename not the complete path
header("Content-Disposition: attachment; filename=".basename($archive_file_name));

// Adding file-size to header
header("Content-Length: " . filesize($archive_file_name));

// Check that is path to zip with filename
readfile($archive_file_name);

感谢您的回复,但不幸的是,basename和内容长度的更改都没有产生任何影响$存档文件名是一个文本变量,模式为“CompanyName”。$uploadDate..zip”;如果$uploadDate的日期格式为“Y-m-d”。$archive\u文件名的示例为“CompanyName2016-02-23.zip”,那么这就是代码中的问题-您需要在$zip->open和readfile的文件名之前添加路径。