udjamaflip
08-11-2009, 02:27 PM
Using the Recursive Directory Listing (Show full directory structure) (http://codingforums.com/showthread.php?t=71882) post, I have amended it slightly to allow a user to fully delete an entire directory recursively, please note this may not work in Safe Mode PHP depending on settings.
<?php
function remDirectory( $path = '.'){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
remDirectory( "$path/$file");
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
unlink("$path/$file");
// Just print out the filename
}
}
}
closedir( $dh );
rmdir($dh);
// Close the directory handle
}
remDirectory('/home/var/public_html/atest/');
?>
Just incase anyone was wondering if the two could be user together you could probably make a nice combination of the two with a 3rd param which specifies what kind of action the function is performing.
Hope it helps someone! :thumbsup:
<?php
function remDirectory( $path = '.'){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
remDirectory( "$path/$file");
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
unlink("$path/$file");
// Just print out the filename
}
}
}
closedir( $dh );
rmdir($dh);
// Close the directory handle
}
remDirectory('/home/var/public_html/atest/');
?>
Just incase anyone was wondering if the two could be user together you could probably make a nice combination of the two with a 3rd param which specifies what kind of action the function is performing.
Hope it helps someone! :thumbsup: