简体   繁体   English

在通过 php 创建 zip 文件时,我得到两个文件而不是一个

[英]On creating zip file by php I get two files instead of one

I'm struggling around with a simple PHP functionality: Creating a ZIP Archive with some files in.我正在为一个简单的 PHP 功能而苦苦挣扎:创建一个包含一些文件的 ZIP 档案。

The problem is, it does not create only one file called filename.zip but two files called filename.zip.a07600 and filename.zip.b07600.问题是,它不仅创建了一个名为 filename.zip 的文件,还创建了两个名为 filename.zip.a07600 和 filename.zip.b07600 的文件。 Pls.请。 see the following screenshot:请参阅以下屏幕截图:

不评论,随便看看。。。

The two files are perfect in size and I even can rename each of them to filename.zip and extract it without any problems.这两个文件大小完美,我什至可以将它们中的每一个重命名为 filename.zip 并毫无问题地提取它。

Can anybody tell me what is going wrong???谁能告诉我出了什么问题???

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;
}

The code which calls the function is a file with a switch-case... it is called itself by an ajax-call:调用该函数的代码是一个带有 switch-case 的文件......它被 ajax 调用调用:

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;

One more code... I tried to rename the latest 123456.zip.a01234 file to 123456.zip and then unlink the old 123456.zip.a01234 (and all prior added .a01234 files) with this function:另一个代码...我尝试将最新的 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);
        }
    }
}

In short: copy works, rename didn't work and "unlink"-doesn't work at all... I'm out of ideas now... :(简而言之:复制有效,重命名无效并且“取消链接” - 根本无效......我现在没有想法...... :(

ONE MORE TRY: I placed the output of $zip->getStatusString() in a variable and wrote it to a log file... the log entry it produced is: Renaming temporary file failed: No such file or directory .再试一次我将 $zip->getStatusString() 的输出放在一个变量中并将其写入日志文件......它产生的日志条目是:重命名临时文件失败:没有这样的文件或目录 But as you can see in the graphic above the file 43051221.zip.a07200 is located in the directory where the zip-lib opens it temporarily.但是正如您在上图中所看到的,文件 43051221.zip.a07200 位于 zip-lib 临时打开它的目录中。

Thank you in advance for your help!预先感谢您的帮助!

So, after struggling around for days... It was so simple:所以,在挣扎了几天之后......它是如此简单:

Actually I work ONLY on *nix Servers so in my scripts I created the folders dynamically with 0777 Perms.实际上我只在 *nix 服务器上工作,所以在我的脚本中,我使用 0777 Perms 动态创建了文件夹。 I didn't know that IIS doesn't accept this permissions format at all!我不知道 IIS 根本不接受这种权限格式!

So I remoted to the server, right clicked on the folder Documents (the hierarchically most upper folder of all dynamically added files and folders) and gave full control to all users I found .所以我远程连接到服务器,右键单击文件夹 Documents (所有动态添加的文件和文件夹的分层最上层文件夹)并完全控制我找到的所有用户

Now it works perfect!!!现在它完美运行!!! The only thing that would be interesting now is: is this dangerous of any reason???现在唯一有趣的是:这是出于任何原因的危险吗???

Thanks for your good will answers...感谢您的善意回答...

My suspicion is that your script is hitting the PHP script timeout.我怀疑您的脚本遇到了 PHP 脚本超时。 PHP zip creates a temporary file to zip in to where the filename is yourfilename.zip.some_random_number. PHP zip 创建一个临时文件以将其压缩到文件名为 yourfilename.zip.some_random_number 的位置。 This file is renamed to yourfilename.zip when the zip file is closed.当 zip 文件关闭时,此文件将重命名为 yourfilename.zip。 If the script times out it will probably just get left there.如果脚本超时,它可能会留在那里。

Try reducing the number of files to zip, or increasing the script timeout with set_time_limit()尝试减少要压缩的文件数量,或使用 set_time_limit() 增加脚本超时

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM