Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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将文件添加到zip_Php_Download_Zip - Fatal编程技术网

使用php将文件添加到zip

使用php将文件添加到zip,php,download,zip,Php,Download,Zip,我创建zip文件的php代码是: function zipFilesDownload($file_names,$archive_file_name,$file_path) { $zip = new ZipArchive(); if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) { exit("cannot open <$archive_file_name>\n");

我创建zip文件的php代码是:

function zipFilesDownload($file_names,$archive_file_name,$file_path)
{
    $zip = new ZipArchive();

    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }

    foreach($file_names as $files)
    {
      if (!file_exists($files)) { die($files.' does not exist'); }
      if (!is_readable($files)) { die($files.' not readable'); }
        $zip->addFile($file_path.$files,$files);
    }
    $zip->close();

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name"); 
    exit;
}
if ($act=="backup") {
$fileNames=array('us/man.txt');
$zip_file_name='myFile.zip';
$file_path=dirname(__FILE__).'/';
zipFilesDownload($fileNames,$zip_file_name,$file_path);
}

但是,如果我将它添加到另一个php中,zip文件将正确下载。

我认为我没有遇到真正的问题。让我们看看:相同的代码在某个地方工作,而在其他地方不工作?如果是这样,给我们看这个“某处”,还有“其他地方”。区别就在这里,不是在这段代码中。这是工作,而是在“其他地方”的ZIP文件中读取,从而生成垃圾文本,而不是下载!但这两种情况下的代码都是一样的,不是吗?如果是,问题是:a)“某处”中的其他代码与“其他地方”中的代码不同。(相同的代码应该总是做相同的事情。)b)“某处”和“其他地方”所在的位置。(您使用的是相对位置,因此可能存在问题)。另一个问题:您的服务器是否显示“通知”/“警告”?也许一个隐藏的通知是你问题的关键。
PK0�~E1x��us/man.txtKIPK0�~E1x��users/mohammad-ali/mamad.txtPKJ>