【问题标题】:Getting files from a directory and adding them to a zip file从目录中获取文件并将它们添加到 zip 文件中
【发布时间】:2013-07-17 16:51:02
【问题描述】:

我创建了一个名为“files.zip”的 zip 文件。我的 property_image 文件夹中有图像。我已经扫描了该目录并拥有我需要的文件类型。 '$prop_image' 是另一个具有图像名称的数组。如果图像文件(位于变量“$file1”中)在该数组中,那么我正在尝试将文件添加到 zip 文件中。

        $zip = new ZipArchive();
        $filename = "files.zip";

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

        $dir = './property_image';
        $file1 = scandir($dir);
        foreach($file1 as $feel){
            $userfile_extn = substr($feel, strrpos($feel, '.')+1);
            if($userfile_extn === 'jpg' || $userfile_extn === 'png' || $userfile_extn === 'gif' || $userfile_extn === 'jpeg'){
                if(in_array($feel, $prop_image)){

                    $zip->addFile("$feel", basename($feel));
                }

            } // end of if($extension ===
        }

        $zip->addFile("$File", basename($File));
        $zip->close();

如果我注释掉该代码,此代码只会将“$File”添加到该 zip 文件中:

$zip->addFile("$feel", basename($feel));

我需要从该目录中添加那些位于 zip 文件中变量“$prop_image”内的文件。

我该怎么做?

【问题讨论】:

    标签: php arrays file directory zip


    【解决方案1】:

    我解决了。

    这可能对其他人有帮助,所以我发布了我的答案。那很简单。我只需要给出保存图像的文件夹名称即可。

    像这样::

    $zip->addFile("./property_image/$feel", basename($feel));
    

    代替::

    $zip->addFile("$feel", basename($feel));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多