0

I like to delete all .gif images from subdirectory i tried with glob

but its getting only 1st level subdirectory

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

but i have directory like this

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

How to check in all subdirectory and delete only .gif images ?

Ivan
  • 433
  • 5
  • 16
  • I think this post should help you get the base set. https://stackoverflow.com/questions/11613840/remove-all-files-folders-and-their-subfolders-with-php – prakhar saxena Oct 05 '20 at 06:28

1 Answers1

2

Referring to the answer in the link :

<?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);
       }
   }
?>

To check the content or file type ; we can use 'mime_content_type()'. This method returns 'image/gif' for the gif.

reference link: https://www.php.net/manual/en/function.mime-content-type.php

Using the method 'rrmdir()' as the base reference, we can hierarchically find the gif files and delete them.