delete file of any extension for directory and subdirectories

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    delete file of any extension for directory and subdirectories

    i have made an delete files script which works for only one directory but not sub directory so i want to delete files of same extention from directory and subdirectory


    please help i will pay for it



    PHP Code:
    <?
    
    $dir = 'hmm/';
    
    function scanr($dir){
    
    $arr = glob($dir.'/*.jpg');
    foreach($arr as $vv){
    
    
    
    //check if $vv is a file
    if(is_file($vv)){
    
    //if file, get the filename
    $vx=explode('/',$vv);
    $file=$vx[count($vx)-1];
    
    
    // if no extension delete the file
    unlink($vv);
    // print the deletion message
    echo $vv." deleted!<br>";}else{
    // if $vv is a dir then scan it again for files
    scanr($vv);
    }}
    }
    
    scanr($dir);
    ?>

    #2
    what exactly do u need? A script that deletes a particular group(s) of file in a directory and sub directory?
    Last edited by syrus; 23.12.11, 19:56.

    Comment


      #3
      something like this?

      PHP Code:
      $dir = 'path/hmm';  //without last slash
      $pattern = '*.jpg';
      gum_remove_files_in_dir($dir,$pattern);
      
      function gum_remove_files_in_dir($dir,$pattern){ 
      
          $arr = glob($dir.'/'.$pattern); 
          foreach($arr as $file){ 
        if(is_dir($file))
        { 
         gum_remove_files_in_dir($file,$pattern);
         rmdir($file); 
         echo $file.' deleted successfully!<br/>';
        }
        else
        { 
         unlink($file);
         echo $file.' deleted successfully!<br/>';
        }
          } 
      }
      
      ?> 
      
      i havent tested the script, but i think it should work.
      Advertise your mobile site for FREE with AdTwirl

      Comment


        #4
        Nice GumSlone, but if we don't want to use recursion, stack would be good choice. btw, you should use unlink instead of rmdir to delete file.
        like this:

        PHP Code:
        <?php
        function ksgRemoveFiles($dir,$pattern)
        {
          $stack=array();
          array_push($stack,$dir);
          while(true){
            $dir=array_pop($stack);
            if($dir==NULL)
              break;
            $a= glob($dir.'/'.$pattern);
            foreach($a as $file) {
              if(is_dir($file)) {
                array_push($stack,$file);
                continue;
              }
              unlink($file);
              echo "File: ".$file." has been removed. <br />";
            }
          }
        }
        ?>
        Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

        Comment


          #5
          guys both scripts are only working for 1 directory but not for subdirectory

          Comment


            #6
            Originally posted by desikida View Post
            guys both scripts are only working for 1 directory but not for subdirectory
            you are right, glob with pattern wouldnt show subdirectories, the code should look a bit different.

            PHP Code:
            <?php
            
            
            $dir = 'path/hmm';  //without last slash
            $pattern = '*.jpg';
            gum_remove_files_in_dir($dir,$pattern);
            
            function gum_remove_files_in_dir($dir,$pattern)
            { 
                $arr = glob($dir.'/'.$pattern); 
                foreach($arr as $file)
                { 
              unlink($file);
              echo $file.' deleted successfully!<br/>';
                }
                
                
                if ($handle = opendir($dir))
                {
              while (false !== ($file = readdir($handle)))
              {
               if ($file != "." && $file != "..") 
               {
                if(is_dir($dir.'/'.$file))
                {
                 gum_remove_files_in_dir($dir.'/'.$file,$pattern);
                }
               }
              }
              closedir($handle);
                }
            }
            
            ?>
            Advertise your mobile site for FREE with AdTwirl

            Comment


              #7
              Thanx Gumslone its working now..

              Comment


                #8
                by the same method can we extract zip files by pclzip.lib.php from directory and subdirectory

                if yes then plz help me

                Comment


                  #9
                  Originally posted by desikida View Post
                  by the same method can we extract zip files by pclzip.lib.php from directory and subdirectory

                  if yes then plz help me
                  yes, like this:


                  PHP Code:
                  <?php
                  
                  
                  $dir = 'path/hmm';  //without last slash
                  $pattern = '*.zip';
                  gum_remove_files_in_dir($dir,$pattern);
                  
                  function gum_remove_files_in_dir($dir,$pattern)
                  { 
                      $arr = glob($dir.'/'.$pattern); 
                      foreach($arr as $file)
                      { 
                    //PUT YOUR ZIP EXTRACTION CODE HERE
                      }
                      
                      
                      if ($handle = opendir($dir))
                      {
                    while (false !== ($file = readdir($handle)))
                    {
                     if ($file != "." && $file != "..") 
                     {
                      if(is_dir($dir.'/'.$file))
                      {
                       gum_remove_files_in_dir($dir.'/'.$file,$pattern);
                      }
                     }
                    }
                    closedir($handle);
                      }
                  }
                  
                  ?>
                  Advertise your mobile site for FREE with AdTwirl

                  Comment


                    #10
                    if have putted
                    PHP Code:
                     include 'pclzip.lib.php';
                    $zip = new PclZip($file);
                    if(!$zip->extract(PCLZIP_OPT_PATH, $dir)) 
                    
                    but its not working

                    its working for only 1 file and giving me error
                    Code:
                    Fatal error : Cannot redeclare PclZipUtilPathReduction() (previously declared in /home/username/public_html/pclzip.lib.php:5893)
                    Last edited by desikida; 25.12.11, 06:13.

                    Comment


                      #11
                      Originally posted by desikida View Post
                      if have putted
                      PHP Code:
                       include 'pclzip.lib.php';
                      $zip = new PclZip($file);
                      if(!$zip->extract(PCLZIP_OPT_PATH, $dir)) 
                      
                      but its not working

                      its working for only 1 file and giving me error
                      Code:
                      Fatal error : Cannot redeclare PclZipUtilPathReduction() (previously declared in /home/username/public_html/pclzip.lib.php:5893)

                      PHP Code:
                      <?php
                      
                      include 'pclzip.lib.php';
                      $dir = 'path/hmm';  //without last slash
                      $pattern = '*.zip';
                      gum_remove_files_in_dir($dir,$pattern);
                      
                      function gum_remove_files_in_dir($dir,$pattern)
                      { 
                          $arr = glob($dir.'/'.$pattern); 
                          foreach($arr as $file)
                          { 
                        //PUT YOUR ZIP EXTRACTION CODE HERE
                      $zip = new PclZip($file);
                      $zip->extract(PCLZIP_OPT_PATH, $dir);
                          }
                          
                          
                          if ($handle = opendir($dir))
                          {
                        while (false !== ($file = readdir($handle)))
                        {
                         if ($file != "." && $file != "..") 
                         {
                          if(is_dir($dir.'/'.$file))
                          {
                           gum_remove_files_in_dir($dir.'/'.$file,$pattern);
                          }
                         }
                        }
                        closedir($handle);
                          }
                      }
                      
                      ?>
                      Advertise your mobile site for FREE with AdTwirl

                      Comment

                      Working...
                      X