Home > Backend Development > PHP Tutorial > Several code examples of PHP recursively deleting directories_PHP tutorial

Several code examples of PHP recursively deleting directories_PHP tutorial

WBOY
Release: 2016-07-13 10:31:59
Original
878 people have browsed it

Here are some function references for you.

Example 1:

Copy code The code is as follows:

function deletedir($dir){
if (!handle=@opendir($dir)){ //Check whether the directory to be opened exists
die("There is no such directory");
}
while(false !==($file=readdir ($handle))){
If($file!=="."&&$file!==".."){ //Exclude the current directory and parent directory
$file=$dir. DIRECTORY_SEPARATOR. $file;
deletedir($file);
if(is_dir($file)){                                                                                                                                                                                                                                                                                   ; }else{
                                                                                                                                                                    Failed!
";
                                              echo "Directory$dirDelete Successful.
n";
}
}

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




Example 2: Recursive directory deletion method in Php (compatible with Chinese directories and files)





Copy code
The code is as follows:


/*
This function supports deleting directories in Chinese. Since I only tested deletion under window, Linux has not been tested. If you have any questions, please leave a message or modify it yourself. .
If there are any mistakes, 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.' return ;

}

foreach($handle_object as $val){

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

If(filetype($tmp_path.'/'.$val)=='dir'){

If(count(scandir($tmp_path.' /not)) == 2) {
IF (RMDIR ($ tmp_path. '/'. $ Val)) {
echo $ $ tmp_path. '/'. $ val. '& lt; br /& gt; ';
} else {
echo $ $ tmp_path.' /'. $ value.' & lt; br /& gt; '; $tmp_path.'/'.$val);
                                                                                                                                                    echo $$tmp_path.'/ '. $ value.' & lt; br /& gt; ';
} else {
echo $ $ tmp_path.' /'. $ value.' & lt; br /& gt; '; >                                                                                                                                    if(rmdir($tmp_path)){
echo $ tmp_path.'
';
}else{

}

delete_dir('D:/AppServ/www/testing/haha');

?>




Example 3: Parameter $dir file name example: admin/runtime like this





Copy code

The code is as follows:

//Delete directory and contained files function
function deldir($dir) {
//Open file directory
$dh = opendir($dir);
// Loop through reading files
while ($file = readdir($dh)) {
if($file != '.' && $file != '..') {
$fullpath = $dir . '/' . $file;

// Determine if it is a directory
if(!is_dir($fullpath)) {
echo $fullpath."Deleted
";
                                                                                                                                             } else {                                                                                                                   >                                                                   dir ($dir)) {
// return true;
// } else {
// return false;
// }
}




Example 4:





Copy code

The code is as follows:

function deldir($dirname){ if (file_exists($dirname)){//First determine whether the directory is valid

                                                                                                                                         Use readdir to loop through the contents of the directory

if($filename != "." && $filename != ".."){//Exclude the two special directories "." and ".."

                                                                                                                                                 Use recursion to delete subdirectories                                                        >         closedir($dir);//Close the file Operation handle rmdir($dirname);//Delete directory } } ?>




Example 5:





Copy code

The code is 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 6:

Copy code The code is as follows:

function del_dir( $dir )
{
if ( $handle = opendir( $dir ) )
{
while ( false !== ( $item = readdir( $handle ) ) )
{
if ( $item != "." && $item != ".." )
                                                                                            ;
                                                                                          ( $handle );
rmdir( $dir ) ;

}
}
?>





http://www.bkjia.com/PHPjc/756995.html


www.bkjia.com

true

http: //www.bkjia.com/PHPjc/756995.html

TechArticle

Related labels:
php
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