PHP delete file and folder operations using the two functions unlink() and rmdir()_PHP tutorial

WBOY
Release: 2016-07-21 15:26:23
Original
1122 people have browsed it

Take a look at the code first

Copy the code The code is as follows:

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);
}
}
}
closedir($dh);
//Delete the current folder:
if( rmdir($dir)) {
return true;
} else {
return false;
}
}
?>

unlink( ) function is used to delete files. Returns true if successful, false if failed. The rmdir() function is used to delete empty directories. It attempts to delete the directory specified by dir. The directory must be empty and must have appropriate permissions.
An example: delete all ".svn" folders under a certain folder (including their contents).
Copy code The code is as follows:

function delsvn($dir) {
$ dh=opendir($dir);
//Find all ".svn" folders:
while ($file=readdir($dh)) {
if($file!=". " && $file!="..") {
$fullpath=$dir."/".$file;
if(is_dir($fullpath)) {
if($file==" .svn"){
delsvndir($fullpath);
}else{
delsvn($fullpath);
}
}
}
}
closedir( $dh);
}
function delsvndir($svndir){
//Delete the files in the directory first:
$dh=opendir($svndir);
while($file= readdir($dh)){
if($file!="."&&$file!=".."){
$fullpath=$svndir."/".$file;
if (is_dir($fullpath)){
delsvndir($fullpath);
}else{
unlink($fullpath);
}
}
}
closedir($ dh);
//Delete directory folder
if(rmdir($svndir)){
return true;
}else{
return false;
}
}
$dir=dirname(__FILE__);
//echo $dir;
delsvn($dir);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323984.htmlTechArticleLook at the code first. Copy the code as follows: ? function deldir($dir) { //Delete the directory first File: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." $file!="..") {...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!