【问题标题】:On creating zip file by php I get two files instead of one在通过 php 创建 zip 文件时,我得到两个文件而不是一个
【发布时间】:2016-02-20 03:19:44
【问题描述】:

我正在为一个简单的 PHP 功能而苦苦挣扎:创建一个包含一些文件的 ZIP 存档。

问题是,它不会只创建一个名为 filename.zip 的文件,而是创建两个名为 filename.zip.a07600 和 filename.zip.b07600 的文件。请。请看以下截图:

这两个文件大小完美,我什至可以将每个文件重命名为 filename.zip 并毫无问题地解压缩。

谁能告诉我怎么回事???

function zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path = array(), $files_array = array()) {
    // Archive File Name
    $archive_file = $archiveDir."/".$archive_file_name;
    // Time-to-live
    $archiveTTL = 86400; // 1 day
    // Delete old zip file
    @unlink($archive_file);
    // Create the object
    $zip = new ZipArchive();
    // Create the file and throw the error if unsuccessful
    if ($zip->open($archive_file, ZIPARCHIVE::CREATE) !== TRUE) {
        $response->res = "Cannot open '$archive_file'";
        return $response;
    }
    // Add each file of $file_name array to archive
    $i = 0;
    foreach($files_array as $value){
        $expl = explode("/", $value);
        $file = $expl[(count($expl)-1)];
        $path_file = $file_path[$i] . "/" . $file;
        $size = round((filesize ($path_file) / 1024), 0);
        if(file_exists($path_file)){
            $zip->addFile($path_file, $file);
        }
        $i++;
    }
    $zip->close();  
    // Then send the headers to redirect to the ZIP file
    header("HTTP/1.1 303 See Other"); // 303 is technically correct for this type of redirect
    header("Location: $archive_file");
    exit;
}

调用该函数的代码是一个带有 switch-case 的文件......它由 ajax-call 自己调用:

case "zdl":
    $files_array = array();
    $file_path = array();
    foreach ($dbh->query("select GUID, DIRECTORY, BASENAME, ELEMENTID from SMDMS where ELEMENTID = ".$osguid." and PROJECTID = ".$osproject.";") as $subrow) {
        $archive_file_name = $subrow['ELEMENTID'].".zip";
        $archiveDir = "../".$subrow['DIRECTORY'];
        $files_array[] = $archiveDir.DIR_SEPARATOR.$subrow['BASENAME'];
        $file_path[] = $archiveDir;
    }
    zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path, $files_array);
break;

还有一个代码...我尝试将最新的 123456.zip.a01234 文件重命名为 123456.zip,然后使用此函数取消链接旧的 123456.zip.a01234(以及所有之前添加的 .a01234 文件):

function zip_file_exists($pathfile){
    $arr = array();
    $dir = dirname($pathfile);
    $renamed = 0;
    foreach(glob($pathfile.'.*') as $file) {
        $path_parts = pathinfo($file);
        $dirname = $path_parts['dirname'];
        $basename = $path_parts['basename'];
        $extension = $path_parts['extension'];
        $filename = $path_parts['filename'];
        if($renamed == 0){
            $old_name = $file;
            $new_name = str_replace(".".$extension, "", $file);
            @copy($old_name, $new_name);
            @unlink($old_name);
            $renamed = 1;
            //file_put_contents($dir."/test.txt", "old_name: ".$old_name." - new_name: ".$new_name." - dirname: ".$dirname." - basename: ".$basename." - extension: ".$extension." - filename: ".$filename." - test: ".$test);
        }else{
            @unlink($file);
        }
    }
}

简而言之:复制作品,重命名不起作用并且“取消链接” - 根本不起作用......我现在没有想法...... :(

再试一次:我将 $zip->getStatusString() 的输出放在一个变量中并将其写入日志文件...它产生的日志条目是:重命名临时文件失败:没有这样的文件或目录。 但正如您在上图中看到的那样,文件 43051221.zip.a07200 位于 zip-lib 临时打开它的目录中。

提前感谢您的帮助!

【问题讨论】:

  • 你是怎么调用这个函数的?它是否在任何循环中调用?
  • 感谢您的好主意。但是该函数只被调用一次。在其他情况下——文件由 scandir 提供——它工作得很好。在这种情况下,我从数据库中挑选文件和路径。

标签: php file-extension php-zip-archive


【解决方案1】:

所以,在苦苦挣扎了几天之后……就这么简单:

实际上我只在 *nix 服务器上工作,所以在我的脚本中,我使用 0777 Perms 动态创建了文件夹。我不知道 IIS 根本不接受这种权限格式!

所以我远程连接到服务器,右键单击文件夹 Documents(所有动态添加的文件和文件夹的分层最上层文件夹)并完全控制我找到的所有用户。

现在完美了!!!现在唯一有趣的是:这有什么危险吗???

感谢您的善意回答...

【讨论】:

  • 具体来说:需要的Windows安全权限是Modify。仅读写是不够的。
【解决方案2】:

我怀疑您的脚本正在达到 PHP 脚本超时。 PHP zip 创建一个临时文件以压缩到文件名为 yourfilename.zip.some_random_number 的位置。当 zip 文件关闭时,此文件将重命名为 yourfilename.zip。如果脚本超时,它可能会被留在原地。

尝试减少要压缩的文件数量,或使用 set_time_limit() 增加脚本超时

http://php.net/manual/en/function.set-time-limit.php

【讨论】:

  • 感谢您的建议,但这不是问题所在:我使用两个测试用例,一个有 2 个文件,另一个有 4 个文件。如您在上面的屏幕截图中所见,这些文件的大小约为 700 Kb...
  • 有趣。我已经复制了你的函数并在本地运行它没有问题。你能分享调用它的代码吗?
  • 我在上面的原始帖子中添加了负责调用函数的代码片段。最有趣的事实是,在我在上面的函数中添加 DB 功能之前,它已经完美运行了......
猜你喜欢
  • 1970-01-01
  • 2012-10-16
  • 2018-08-17
  • 2011-05-09
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多