Summary of how PHP recursively deletes a specified folder

Source: Internet
Author: User

PHP recursively deletes the specified folder method:

Here is a description of the PHP recursive deletion of the specified folder method, the example summarizes two commonly used recursive delete folder skills, very practical value, the need for friends can refer to the

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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 /* Custom Delete function, you can delete file and recursive delete folder * * Function My_del ($path) {if (Is_dir ($path)) {$file _list= scandir ($path); foreach ($file _lis T as $file) {if ($file!= '. ' && $file!= '. ') {My_del ($path. ') /'. $file); }} @rmdir ($path); This method does not need to determine whether the folder is empty,//because regardless of whether the folder is empty at the beginning, it is empty if it arrives here ($path);//These two places are best to use @ Block warning error, look at the annoyance}} $path = ' d :/Technical documents-copy '; folder to delete//if PHP file is not ANSI, but UTF-8 mode,//and the folder you want to delete contains kanji characters, you need transcoding//$path =iconv (' utf-8 ', ' gb2312 ', $path) before calling the function. 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.