PHP recursive delete the specified folder summary _php tips

Source: Internet
Author: User

The example of this article summarizes two methods of PHP recursive deletion of the specified folder. Share to everyone for your reference. Specifically as follows:

Method One:

function Recursivedelete ($dir)
{ 
  if ($handle = @opendir ($dir))
  {while
  ($file = Readdir ($handle))!== False)
  {
   if ($file = = ".") | | ($file = = ".."
   ) {
    continue;
   }
   if (Is_dir ($dir. '/' . $file))
   {
    //call self to this directory
    Recursivedelete ($dir. '/' . $file);
   }
   else
   {
    unlink ($dir. '/' . $file); Remove this file
   }
  }
  @closedir ($handle);
  RmDir ($dir);
  }

Method Two:

/*
 Custom delete function, can delete file and recursive 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 need to determine whether the folder is empty,//Because whether the folder is empty at the beginning or not, it
   is empty  
 }
 else
 {
  @unlink ($path);
  These two places are best to use @ Block warning error, look annoyed
 }
}
$path = ' d:/Technical document-copy ';
folder to delete
//If PHP file is not ANSI, but UTF-8 mode,
//and the folder to be deleted contains kanji characters, the call to the function requires a transcoding
//$path =iconv (' Utf-8 ', ' gb2312 ', $path);
My_del ($path);

I hope this article will help you with your PHP program design.

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.