Forums / WEB Site / Script testing and error fixing
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.
Sort replies:
Oldest
Top voted
Newest
DE
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);
?>
SY
syrus
Junior · 44 posts
14 years ago
#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
14 years ago
#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.
KS
ksg91
Developer · 162 posts
14 years ago
#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 />";
}
}
}
?>
DE
guys both scripts are only working for 1 directory but not for subdirectory
GumSlone
Administrator Principal · 1,690 posts
14 years ago
#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);
}
}
?>
DE
Thanx Gumslone its working now..
DE
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
14 years ago
#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);
}
}
?>
DE
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
14 years ago
#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);
}
}
?>