【问题标题】:getting specific type of file and adding them to an existing zip file获取特定类型的文件并将它们添加到现有的 zip 文件中
【发布时间】:2013-07-12 15:19:51
【问题描述】:
$zip = new ZipArchive();
    $filename = "image/files.zip";

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

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

我已经在 zip 中添加了一个文件,该文件位于变量“$File”中。该文件存在于同一文件夹(图像)中。我需要在同一个 zip 文件中添加图像。这些图像位于“图像”文件夹中。我想我需要扫描图像并将它们添加到同一个 zip 中。但我不知道该怎么做。

如何将特定类型(jpeg、png、gif)的文件添加到现有 zip 文件或新文件中?我必须在我的 zip 文件的根目录中添加图像,而不是在 zip 文件的任何文件夹中。图片位于一个目录('images')中,我需要将它们添加到我的 zip 文件中,该文件也保存在同一目录中。

【问题讨论】:

  • 提供一些代码,因为我不知道如何回答你,没有你想做的具体方式
  • zip_entry_open() zip_entry_close()
  • 我的问题已更新,请查看一次

标签: php image zip directory gd


【解决方案1】:
$zip = new ZipArchive; 
if ($zip->open('source.zip') === TRUE) 
{ 
     for($i = 0; $i < $zip->numFiles; $i++) 
     {   
        $fp = $zip->getStream($zip->getNameIndex($i));
        if(!$fp) exit("failed\n");
        while (!feof($fp)) {
            $contents = fread($fp, 8192);
            // explode for extension and do what ever you want
        }
        fclose($fp);
     }
} 
else 
{ 
     echo 'Error reading zip-archive!'; 
} 

【讨论】:

  • 我需要检查它们是否是图像
【解决方案2】:

我就是这样解决的。

        $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)){
                    //echo $feel;
                    $zip->addFile("./property_image/$feel", basename($feel));
                }

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

        } // end of foreach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多