Home > Backend Development > PHP Tutorial > PHP delete non-empty directory custom function_PHP tutorial

PHP delete non-empty directory custom function_PHP tutorial

WBOY
Release: 2016-07-20 11:16:42
Original
1072 people have browsed it

   //循环删除目录和文件函数

代码如下  

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;
}

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template