//循环删除目录和文件函数
代码如下 |
|
function delDirAndFile( $dirName )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delDirAndFile( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
if( rmdir( $dirName ) )echo “成功删除目录: $dirName
n”;
}
}
?>
代码如下 |
|
function delDirAndFile( $dirName )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delDirAndFile( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
if( rmdir( $dirName ) )echo “成功删除目录: $dirName
n”;
}
}
?>
|
|
//循环目录下的所有文件
代码如下 |
|
function delFileUnderDir( $dirName=”../Smarty/templates/templates_c” )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delFileUnderDir( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
}
}
代码如下 |
|
function delFileUnderDir( $dirName=”../Smarty/templates/templates_c” )
{
if ( $handle = opendir( “$dirName” ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != “.” && $item != “..” ) {
if ( is_dir( “$dirName/$item” ) ) {
delFileUnderDir( “$dirName/$item” );
} else {
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item
n”;
}
}
}
closedir( $handle );
}
}
?>
|
?>
|
目录删除函数
代码如下 |
|
function my_del_dir($directory)
代码如下 |
|
function my_del_dir($directory)
{
if (is_dir($directory) == false)
{
exit("The Directory Is Not Exist!");
}
$handle = opendir($directory);
while (($file = readdir($handle)) !== false)
{
if ($file != "." && $file != "..")
{
is_dir("$directory/$file")?
my_del_dir("$directory/$file"):
unlink("$directory/$file");
}
}
if (readdir($handle) == false)
{
closedir($handle);
rmdir($directory);
}
}
?>
|
{
if (is_dir($directory) == false)
{
exit("The Directory Is Not Exist!");
}
$handle = opendir($directory);
while (($file = readdir($handle)) !== false)
{
if ($file != "." && $file != "..")
{
is_dir("$directory/$file")?
my_del_dir("$directory/$file"):
unlink("$directory/$file");
}
}
if (readdir($handle) == false)
{
closedir($handle);
rmdir($directory);
}
}
?>
|
One written by myself
$sourceDir The directory path to be deleted
Call the method DeleteDir(“images/abc”); //Delete the abc directory
代码如下 |
|
function DeleteDir($sourceDir){
$succeed = true;
if(file_exists($sourceDir)){
$objDir = opendir($sourceDir);
while(false !== ($fileName = readdir($objDir))){
if(($fileName != “.”) && ($fileName != “..”)){
chmod(“$sourceDir/$fileName”,0777);
if(!is_dir(“$sourceDir/$fileName”)){
if(!unlink(“$sourceDir/$fileName”)){
$succeed = false;
break;
}
}
else{
DeleteDir(“$sourceDir/$fileName”);
}
}
}
if(!readdir($objDir)){
closedir($objDir);
if(!rmdir($sourceDir)){
$succeed = false;
}
}
}
return $succeed;
}
|
http://www.bkjia.com/PHPjc/372607.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372607.htmlTechArticle//The function code for loop deletion of directories and files is as follows function delDirAndFile( $dirName ) { if ( $handle = opendir( $dirName ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if...