How PHP implements code summarization using recursive delete files

Source: Internet
Author: User
Introduced several PHP uses the recursive deletion file implementation code, hoped to be helpful to the friend's PHP study.

Loop + recursion

<?php deltree ('./copy copy of duplicate copy of duplicate copy of duplicate copy of the re-piece of AAA '); function deltree ($pathdir) {//echo $pathdir. ' <br/> ';//When I debug the IF (Is_empty_dir ($pathdir))//If it is empty {rmdir ($pathdir);//directly Delete} else {//otherwise read this directory except. and. External $d =dir ($pathdir); while ($a = $d->read ())//Under Delete only $pathdir under {if (Is_file ($pathdir. ') /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {unlink ($pathdir. ') /'. $a); If it is a file, delete}elseif directly (Is_dir ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) If the directory {if (!is_empty_dir ($pathdir. ') /'. $a))//Is empty {deltree ($pathdir. ' /'. $a); If not, call itself}else {rmdir ($pathdir. ' /'. $a); If it is empty directly delete}}} $d->close (); echo "must first delete all files in the directory";//RmDir ($PATHDIR) used for debugging; }} function Is_empty_dir ($pathdir) {//To determine if the directory is empty, my method is not very good? Something else is not empty $d =opendir ($pathdir); $i = 0; while ($a =readdir ($d)) {$i + +;} closedir ($d); if ($i >2) {return false;} else return true; }?>

Recursive method

<?php header ("content-type:text/html; charset=gb2312 "); if (Deletedir (')/Copy copy duplicate copy duplicate copy copy copy copy duplicate copy piece re-copy of (AAA ')) echo "Delete succeeded"; function Deletedir ($dir) {if (@rmdir ($dir) ==false && is_dir ($dir))//deleted, go to delete all files {if ($DP = Opendir ($dir)) {W Hile (($file =readdir ($DP))! = False) {if ($file! = '. ' && $file! = ' ... ') {//echo $file = $dir. '/'. $file; Echo ' <br/> '; $file = $dir. '/'. $file; if (Is_dir ($file))//is the real directory {Deletedir ($file);} else {unlink ($file);}} } closedir ($DP); }else {return false;}} if (Is_dir ($dir) && @rmdir ($dir) ==false)//is the directory is not deleted return false; return true; }?>

Two more recursion methods are introduced:

function Recursivedelete ($dir) {  if ($handle = @opendir ($dir))  {while  (($file = Readdir ($handle))!== false  {   if ($file = = ") | | ($file = = ".."))   {    continue;   }   if (Is_dir ($dir. '/' . $file))   {    //call-self-for-this directory    recursivedelete ($dir. '/' . $file);   }   else   {    unlink ($dir. '/' . $file); Remove this file   }  }  @closedir ($handle);  RmDir ($dir);  }}
/* Custom Delete function, can delete file and recursively delete folder */function My_del ($path) {if (Is_dir ($path)) {   $file _list= scandir ($path);   foreach ($file _list as $file)   {    if ($file! = '. ' && $file! = ')    {     My_del ($path. ') /'. $file);    }   }   @rmdir ($path);   This method does not have to determine whether the folder is empty,   //Because the folder is empty at the beginning, when it arrives here, it is empty  } else {  @unlink ($path);  These two places best still need to use @ Shield warning error, look at the annoyance}} $path = ' d:/technical documents-copy ';//the folder to be deleted//if the PHP file is not ANSI, but UTF-8 mode,//and the folder to be deleted contains Chinese characters, Transcoding required before calling function//$path =iconv (' utf-8 ', ' gb2312 ', $path); My_del ($path);

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.