To delete a directory, we must first delete the files in the directory. Deleting files in PHP is very simple with unlink(). To delete an empty directory, use rmdir.
Example 1
The code is as follows | Copy code |
代码如下 | 复制代码 |
function del_dir($dir){ { if (!$dir) { return ; } $cacheDir = $dir; $dh = opendir($cacheDir); while ( $file = readdir($dh) ) {
if (($file == '.') || ($file == '..')) { continue; }
if (file_exists( $cacheDir .'/'.$file)) { if(is_dir( $cacheDir .'/'.$file)){ del_dir($cacheDir .'/'.$file); }elseif (!unlink($cacheDir .'/'. $file)) {
//删除完操作 } } } } }
| function del_dir($dir){ { if (!$dir) { return ; } $cacheDir = $dir ; $dh = opendir($cacheDir); while ( $file = readdir($dh) ) {if (($file == '.') || ( $file == '..')) { continue; } 代码如下 | 复制代码 | function deldir($dir) { //先删除目录下的文件: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } } closedir($dh); //删除当前文件夹: if(rmdir($dir)) { return true; } else { return false; } }
?> |
if (file_exists( $cacheDir .'/'.$file)) { if(is_dir( $cacheDir .'/' .$file)){ del_dir($cacheDir .'/'.$file); }elseif (!unlink($cacheDir .'/'. $file)) {
代码如下 | 复制代码 | function removeDir($dirName) { if(!is_dir($dirName)) //如果传入的参数不是目录,则为文件,应将其删除 { @unlink($dirName);//删除文件 return false; } $handle = @opendir($dirName); //如果传入的参数是目录,则使用opendir将该目录打开,将返回的句柄赋值给$handle while(($file = @readdir($handle)) !== false) //这里明确地测试返回值是否全等于(值和类型都相同)FALSE,否则任何目录项的名称求值为 FALSE 的都会导致循环停止(例如一个目录名为“0”)。 { if($file!='.'&&$file!='..') //在文件结构中,都会包含形如“.”和“..”的向上结构,但是它们不是文件或者文件夹 { $dir = $dirName . '/' . $file; //当前文件$dir为文件目录+文件 is_dir($dir)?removeDir($dir):@unlink($dir); //判断$dir是否为目录,如果是目录则递归调用reMoveDir($dirName)函数,将其中的文件和目录都删除;如果不是目录,则删除该文件 } } closedir($handle); return rmdir($dirName) ; } | //Delete operation completed } } } } } |
Example 2
The code is as follows | Copy code |
function deldir($dir) { //Delete the files in the directory first: $dh=opendir($dir) ; while ($file=readdir($dh)) { if($file!="." && $file!="..") { $fullpath=$dir ."/".$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath);< br /> } } } closedir($dh); //Delete the current folder: if(rmdir($dir) ) { return true; } else { return false; } }<🎜><🎜>?> |
Example 3
The code is as follows | Copy code |
function removeDir($dirName) { If(!is_dir($dirName)) //If passed The parameters entering are not the directory, and the file is the file. You should delete it. $handle = @opendir($dirName); //If the parameter passed in is a directory, use opendir to open the directory, and assign the returned handle to $handle while(($file = @readdir($ handle)) !== false) //Here we explicitly test whether the return value is equal to (the same value and type) FALSE, otherwise the name of any directory entry that evaluates to FALSE will cause the loop to stop (for example, a directory named " 0"). { if ($ file! = '.' && $ file! = '..') // In the file structure, it will contain the upward structure such as "." and ".." , But they are not files or folders { $ DIR = $ DIRNAME. ' /'. $ File; // The current file $ DIR is the file directory+file IS_DIR ($ dir) ?removeDir($dir):@unlink($dir); //Determine whether $dir is a directory. If it is a directory, call the reMoveDir($dirName) function recursively to delete all the files and directories in it; if it is not a directory, then Delete the file |
Example 4
Delete a directory created a few days ago
The code is as follows 代码如下 | 复制代码 | function delfile($dir,$n) //删除DIR路径下N天前创建的所有文件; { if(is_dir($dir)) { if($dh=opendir($dir)) { while (false !== ($file = readdir($dh))) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; if(!is_dir($fullpath)) { $filedate=date("Y-m-d", filemtime($fullpath)); $d1=strtotime(date("Y-m-d")); $d2=strtotime($filedate); $Days=round(($d1-$d2)/3600/24); if($Days>$n) unlink($fullpath); ////删除文件 } } } } closedir($dh); } } ?> | |
Copy code |
|
function delfile($dir,$n) //Delete all files created N days ago under the DIR path File;{
if(is_dir($dir))
{ if($dh=opendir($dir)) { while (false !== ($ file = readdir($dh))) { if($file!="." && $file!="..") { $fullpath=$dir."/ ".$file; if(!is_dir($fullpath)) { $filedate=date("Y-m-d", filemtime($fullpath)); $d1=strtotime(date( "Y-m-d"));
unlink($fullpath); ////Delete file
} }<🎜> }<🎜> closedir($dh); <🎜> }<🎜> }<🎜>?>
http://www.bkjia.com/PHPjc/444635.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444635.htmlTechArticleTo delete a directory we must first delete the files in the directory. Deleting files in php is very simple unlink() , just use rmdir to delete empty directories. Example 1 The code is as follows Copy the code function d...