【问题标题】:How to extract only image file and delete rest of the file如何仅提取图像文件并删除文件的其余部分
【发布时间】:2018-03-19 17:05:01
【问题描述】:

我试图在我的服务器上提取一个 zip 文件,有时这些文件包含 MAC_STORE 和其他用户不应放入 zip 文件的格式。

这是我尝试过的

public static function extractItem($path, $ext_path) {
        $zip = new ZipArchive;

        $zip->open($path);
        $filename = array();

    for ($i = 0; $i < $zip->numFiles; $i++) {
        $stat = $zip->statIndex($i);
        if ($stat['size'] != 0) {
            $filename[] = basename($stat['name']);
        }
    }
  }

如果 zip 文件包含 .jpg,jpeg,png 那么没关系,如果它包含其他格式我想删除它们

这是$filename array的回复

Array
 (
   [0] => Screen Shot 2017-10-04 at 12.38.47 AM.png
   [1] => ._Screen Shot 2017-10-04 at 12.38.47 AM.png
   [2] => Screen Shot 2017-10-04 at 12.38.50 AM.png
   [3] => ._Screen Shot 2017-10-04 at 12.38.50 AM.png
 )

在 zip 文件中只有两张图片

【问题讨论】:

  • PHP_EOL 在里面做什么?文件名不是文本文件...您在哪里检查代码中的文件扩展名?

标签: php zip unzip


【解决方案1】:

您只能提取扩展名为 .jpg,jpeg,png 的文件,此处解释为: php zipArchive unzip only certain extensions

但是你可以用这个替换正则表达式查找器:

preg_match('#.+([jpg|jpeg|png])$#i', $entry)

【讨论】:

  • 我的 zip 文件中有两个文件,循环运行了四次?
猜你喜欢
  • 2021-08-21
  • 1970-01-01
  • 2019-09-09
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多