Php recursive method for deleting a specified folder _ php skills

Source: Internet
Author: User
This article mainly introduces php recursive deletion of specified folders. The example summarizes two common recursive deletion of folders, which is of great practical value, for more information about how to recursively delete a specified folder in php, see the examples in this article. Share it with you for your reference. The details are as follows:

Method 1:

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);  }}

Method 2:

/* Custom deletion function, which can delete files and recursively delete folders */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 be used to determine whether the folder is empty. // no matter whether the folder is empty at the beginning, all are empty} else {@ unlink ($ path); // it is best to use @ to block the warning error in the two places and look at it.} $ path = 'd: /technical documentation-replicas '; // 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 before calling a function // $ path = iconv ('utf-8', 'gb2312', $ path); my_del ($ path );

I hope this article will help you with php programming.

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.