【问题标题】:How to delete all .gif images from subdirectory如何从子目录中删除所有 .gif 图像
【发布时间】:2021-01-20 00:13:15
【问题描述】:

我喜欢从我尝试使用glob 的子目录中删除所有 .gif 图像

但它只有一级子目录

$dirs = array_filter(glob('main/*'), 'is_dir');

但我有这样的目录

Main 
Sub 1
 Sub 1-2
  Sub 1-3
   Sub 1-4
Sub 2
 Sub 2-1
Sub 3
Sub 4
 Sub 4-1
  Sub 4-2
   Sub 4-3

如何检查所有子目录并只删除 .gif 图片?

【问题讨论】:

标签: php


【解决方案1】:

参考链接中的答案:

<?php
    function rrmdir($dir) {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                    // here we can check the mime_content_type for the gif 
                    if (mime_content_type($dir."/".$object) == "image/gif") 
                        rrmdir($dir."/".$object); 
                    else unlink   ($dir."/".$object);
                }
            }
           reset($objects);
           rmdir($dir);
       }
   }
?>

检查内容或文件类型;我们可以使用'mime_content_type()'。 此方法返回 'image/gif' 的 gif。

参考链接:https://www.php.net/manual/en/function.mime-content-type.php

使用 'rrmdir()' 方法作为基础引用,我们可以分层查找 gif 文件并删除它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多