PHP recursive Delete directory Several code instances _php instances

Source: Internet
Author: User
Here are a few function references for you.

Example one:

Copy the Code code as follows:
function Deletedir ($dir) {
if (!handle= @opendir ($dir)) {//detects if the directory to open exists
Die ("No such directory");
}
while (False!== ($file =readdir ($handle))) {
if ($file!== "." && $file!== "..") {//Exclude current directory from parent directory
$file = $dir. Directory_separator. $file;
if (Is_dir ($file)) {
Deletedir ($file);
}else{//Www.php.net
if (@unlink ($file)) {
echo "File $file deleted successfully.
";
}else{
echo "File $file Delete failed!
";
}
}
}
if (@rmdir ($dir)) {
echo "Directory $dir deleted successfully.
\ n ";
}else{
echo "Directory $dir Delete failed!
\ n ";
}
}

Test program
$dir = "/var/www/test";
Deletedir ($dir);
?>

Example two: Php Recursive delete directory method (compatible with Chinese directories and files)

Copy CodeThe code is as follows:
/*
This function supports Chinese delete directory, because I only tested the window under Delete, Linux is not tested, if there is a problem can leave a message or a little change.
If you are wrong, please correct me and learn together.
*/
Header ("content-type:text/html; charset=gb2312 ");

function Delete_dir ($tmp _path) {


if (!is_writable ($tmp _path) && is_dir ($tmp _path)) {
chmod ($tmp _path,0777);
}

$encode = mb_detect_encoding ($tmp _path, Array (' UTF-8 ', ' GB2312 ', ' ASCII ', ' GBK '));
$tmp _path = Iconv ($encode, ' gb2312 ', $tmp _path);


$handle _object = Scandir ($tmp _path);


if (Count (Scandir ($tmp _path)) ==2) {

if (RmDir ($tmp _path)) {
echo $tmp _path. '
';
}else{
echo $tmp _path. '
';
}

return;

}

foreach ($handle _object as $val) {

if ($val! = ': ' && $val! = '. ' && $val! = ') {

if (filetype ($tmp _path. ' /'. $val) = = ' dir ') {

if (Count (Scandir ($tmp _path. ' /'. $val)) ==2) {
if (RmDir ($tmp _path. ' /'. $val)) {
echo $ $tmp _path. ' /'. $val. '
';
}else{
echo $ $tmp _path. ' /'. $val. '
';
}
}else{
Delete_dir ($tmp _path. ' /'. $val);
}
}else{
if (unlink ($tmp _path. ' /'. $val)) {
echo $ $tmp _path. ' /'. $val. '
';
}else{
echo $ $tmp _path. ' /'. $val. '
';
}
}
}else{

Continue
}

}


if (RmDir ($tmp _path)) {
echo $tmp _path. '
';
}else{
echo $tmp _path. '
';
}

return;


}

Delete_dir (' d:/appserv/www/testing/haha ');

?>

Instance three: Parameter $dir file name example: Admin/runtime such a

Copy the Code code as follows:
Delete the directory and the included file functions
function Deldir ($dir) {
Open File Directory
$DH = Opendir ($dir);
Looping through files
while ($file = Readdir ($DH)) {
if ($file! = '. ' && $file! = ' ... ') {
$fullpath = $dir. '/' . $file;

Determine if the directory
if (!is_dir ($fullpath)) {
echo $fullpath. " has been deleted
";
If not, delete the file
if (!unlink ($fullpath)) {
}
} else {
If it is a directory, the recursion itself deletes the subordinate directory
Deldir ($fullpath);
}
}
}
Close Directory
Closedir ($DH);
Delete Directory
if (RmDir ($dir)) {
return true;
} else {
return false;
// }
}

Example four:

Copy the Code code as follows:
function Deldir ($dirname) {
if (file_exists ($dirname)) {//first determine if the directory is valid
$dir = Opendir ($dirname);//Open Directory with Opendir
while ($filename = Readdir ($dir)) {//Use Readdir loop to read contents of directory
if ($filename! = "." && $filename! = "...") {//exclude '. ' and ".." These two special directories
$file = $dirname. " /". $filename;
if (Is_dir ($file)) {//determines if it is a directory, and if yes it calls itself
Deldir ($file); Using recursion to delete subdirectories
}else{
@unlink ($file);//delete files
}
}
}
Closedir ($dir);//close file operation handle
RmDir ($dirname);//delete directory
}
}
?>

Example five:

Copy the Code code as follows:
/**
* Delete Non-empty directories
* @method Rrmdir
*/
function Rrmdir ($dir) {

if (Is_dir ($dir)) {

$fs = Array_slice (Scandir ($dir), 2);
foreach ($fs as $f) {
$path = $dir. '/' . $f;
Is_dir ($path)? Rrmdir ($path): unlink ($path);
}
Reset ($FS);
return rmdir ($dir);
}

}

Example SIX:

Copy the Code code as follows:
function Del_dir ($dir)
{
if ($handle = Opendir ($dir))
{
while (false!== ($item = Readdir ($handle)))
{
if ($item! = "." && $item! = "...")
{
if (Is_dir ("$dir/$item"))
{
Del_dir ("$dir/$item");
}
Else
{
Unlink ("$dir/$item");
}
}
}
Closedir ($handle);
RmDir ($dir);

}
}
?>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.