在$ zip-> extractTo中合并而不是替换

时间:2019-02-14 07:33:54

标签: php

我使用以下代码在上传后解压缩了zip文件夹。

  /* here it is really happening */

    if(move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);  // open the zip file to extract
        if ($x === true) {
            echo $targetdir; exit;
            $zip->extractTo($targetdir); // place in the directory with same name  
            $zip->close();
            unlink($targetzip);
        }
        $message = "Your .zip file was uploaded and unpacked.";
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    } 

工作正常。但是唯一的问题是该文件夹被上载的新文件夹替换。我要合并它。

假设我有一个zip文件夹。 一个.zip  一种  b  c

当我上传此文件时,它应与上传服务器中已经存在的(One)文件夹合并。因此,不应替换(一个)文件夹中的先前文件夹。

我认为现在可以理解了。请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以与目标精细名称关联当前时间和日期:

$targetdir = $targetdir."_".str_replace(' ',"/",date('Y-m-d H:i:s'));

使用此逻辑,您将不会替换为先前的文件。