How to delete a folder in which was started as a script?
I have a delete.php in folder abc so I call localhost/abc/delete.php
. I want to be开发者_如何学C able to delete abc folder and all its contents from server when I call localhost/abc/delete.php
. How to do such thing?
This function deletes a directory with all of it's content. Second parameter is boolean to instruct the function if it should remove the directory or only the content
function rmdir_r ( $dir, $DeleteMe = TRUE )
{
if ( ! $dh = @opendir ( $dir ) ) return;
while ( false !== ( $obj = readdir ( $dh ) ) )
{
if ( $obj == '.' || $obj == '..') continue;
if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
}
closedir ( $dh );
if ( $DeleteMe )
{
@rmdir ( $dir );
}
}
//use like this:
rmdir_r( 'abc' );
- http://www.roscripts.com/snippets/show/170
Try something like this:
function deleteDir($dir) {
if (!is_dir($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDir($dir.'/'.$item)) return false;
}
return rmdir($dir);
}
$dir = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
deleteDir($dir);
精彩评论