note 96514 added to function.readdir
| From: | cesaratsagiedotes at osu1 dot php dot net | Date: | Wed, 03 Mar 2010 11:07:08 +0000 |
| Subject: | note 96514 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166554@lists.php.net to get a copy of this message | ||
Derived from ianatvirante.com's example.
this script removes a file from a folder AND its subfolders. Just in case somebody finds it useful.
Here is the code:
<?php
//define the path as relative
$path = "path to parent folder";
$my_file = "name of file to be deleted";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Delete all $my_file files from folder $path & subfolders<br/>";
list_dir($dir_handle,$path,$my_file);
function list_dir($dir_handle,$path,$my_file)
{
// print_r ($dir_handle);
echo "<ol>";
//running the while loop
while (false !== ($file = readdir($dir_handle))) {
$dir =$path.'/'.$file;
if(is_dir($dir) && $file != '.' && $file !='..' )
{
$handle = @opendir($dir) or die("undable to open file $file");
echo '<li><a href=' . $path . '/' . $file .
'>' .$file . '</a></li>';
list_dir($handle, $dir, $my_file);
}elseif($file != '.' && $file !='..')
{
echo '<li><a href=' . $path . '/' . $file . '>' .$file
. '</a></li>';
if ($file == $my_file){
unlink($path.'/'.$file);
echo $path.'/'.$file. ' deleted.<br/>';
}
}
}
echo "</ol>";
//closing the directory
closedir($dir_handle);
}
?>
----
Server IP: 80.64.47.13
Probable Submitter: 95.214.47.0
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/96514
Del: integrated -- https://master.php.net/note/delete/96514/integrated
Del: useless -- https://master.php.net/note/delete/96514/useless
Del: bad code -- https://master.php.net/note/delete/96514/bad+code
Del: spam -- https://master.php.net/note/delete/96514/spam
Del: non-english -- https://master.php.net/note/delete/96514/non-english
Del: in docs -- https://master.php.net/note/delete/96514/in+docs
Del: other reasons-- https://master.php.net/note/delete/96514
Reject -- https://master.php.net/note/reject/96514
Search -- https://master.php.net/manage/user-notes.php