Here are a few functions for you to reference.
Example one:
Copy Code code as follows:
<?php
function Deletedir ($dir) {
if (!handle= @opendir ($dir)) {//detect if the directory you want to open is present
Die ("No such catalogue");
}
while (False!== ($file =readdir ($handle))) {
if ($file!== "." && $file!== "...") {//Exclude current directory and parent directory
$file = $dir. Directory_separator. $file;
if (Is_dir ($file)) {
Deletedir ($file);
}else{//Www.jb51.net
if (@unlink ($file)) {
echo file <b> $file </b> Delete succeeded. <br> ";
}else{
echo "File <b> $file </b> delete failed!<br>";
}
}
}
if (@rmdir ($dir)) {
echo Directory <b> $dir </b> Delete succeeded. <br>\n ";
}else{
echo Directory <b> $dir </b> Delete failed! <br>\n ";
}
}
Test program
$dir = "/var/www/test";
Deletedir ($dir);
?>
Example two: Php Recursive delete directory method (compatible with Chinese directories and files)
Copy Code code as follows:
<?php
/*
This function supports the deletion of the directory in Chinese, because I only tested the window under the Delete, Linux did not test, if there is a problem can leave a message or a little modification can be.
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. ' <br/> ';
}else{
echo $tmp _path. ' <br/> ';
}
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. ' <br/> ';
}else{
echo $ $tmp _path. ' /'. $val. ' <br/> ';
}
}else{
Delete_dir ($tmp _path. /'. $val);
}
}else{
if (unlink ($tmp _path. ') /'. $val)) {
echo $ $tmp _path. ' /'. $val. ' <br/> ';
}else{
echo $ $tmp _path. ' /'. $val. ' <br/> ';
}
}
}else{
Continue
}
}
if (RmDir ($tmp _path)) {
echo $tmp _path. ' <br/> ';
}else{
echo $tmp _path. ' <br/> ';
}
return;
}
Delete_dir (' d:/appserv/www/testing/haha ');
?>
Instance three: Parameter $dir filename Example: Admin/runtime such a
Copy Code code as follows:
Delete directory and 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;
To determine whether a directory
if (!is_dir ($fullpath)) {
echo $fullpath. " has been deleted <br> ";
If not, delete the file
if (!unlink ($fullpath)) {
}
} else {
If it is a directory, recursively deletes the subordinate directory itself
Deldir ($fullpath);
}
}
}
Close Directory
Closedir ($DH);
Delete Directory
if (RmDir ($dir)) {
return true;
} else {
return false;
// }
}
Example four:
Copy Code code as follows:
<?php
function Deldir ($dirname) {
if (file_exists ($dirname)) {//To first determine whether the directory is valid
$dir = Opendir ($dirname);//Open Directory with Opendir
while ($filename = Readdir ($dir)) {//Use Readdir to iterate through the contents of the directory
if ($filename!= "." && $filename!= "...") {//exclude "." and ".." These two special directories
$file = $dirname. " /". $filename;
if (Is_dir ($file)) {//To determine whether the directory, if it is the call itself
Deldir ($file); Delete subdirectories using recursion
}else{
@unlink ($file);//delete file
}
}
}
Closedir ($dir);//close file action handle
RmDir ($dirname);//delete directory
}
}
?>
Example five:
Copy Code code as follows:
/**
* Delete non-empty directory
* @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 Code code as follows:
<?php
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);
}
}
?>