delete file of any extension for directory and subdirectories

Started on the old forum in December 2011 · view the original in the archive · old links redirect here

10 replies · 881 views · started 14 years ago · 1 min read

TL;DR · summary of 10 replies
  • - - guys both scripts are only working for 1 directory but not for subdirectory
  • - - the solution involves using glob with a pattern to include subdirectories, as suggested by GumSlone.
  • - - desikida needs help in extracting zip files from directories and subdirectories using pclzip.lib.php.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

4 participants
desikida Junior · 13 posts #1

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

<?

$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);
?>
syrus Junior · 44 posts #2

what exactly do u need? A script that deletes a particular group(s) of file in a directory and sub directory?

GumSlone Administrator Principal · 1,690 posts #3

something like this?

$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

External image (may be gone)

ksg91 Developer · 162 posts #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
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

desikida Junior · 13 posts #5

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

GumSlone Administrator Principal · 1,690 posts #6

desikida wrote: 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

$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

External image (may be gone)

desikida Junior · 13 posts #7

Thanx Gumslone its working now..

desikida Junior · 13 posts #8

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

if yes then plz help me

GumSlone Administrator Principal · 1,690 posts #9

desikida wrote: 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

$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

External image (may be gone)

desikida Junior · 13 posts #10

if have putted

 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

Fatal error : Cannot redeclare PclZipUtilPathReduction() (previously declared in /home/username/public_html/pclzip.lib.php:5893)
GumSlone Administrator Principal · 1,690 posts #11

desikida wrote: if have putted

 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

Fatal error : Cannot redeclare PclZipUtilPathReduction() (previously declared in /home/username/public_html/pclzip.lib.php:5893)
<?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

External image (may be gone)

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close