PHP recursive removal of a specified folder method summary _php Tutorial

Source: Internet
Author: User

Php Recursive method Summary for deleting a specified folder


PHP recursively deletes the specified folder method:

below for you to introduce the PHP recursive deletion of the specified folder method, the example summarizes the two commonly used recursive delete folder skills, very practical value, the need for friends can refer to the following

This article summarizes two ways that PHP recursively deletes a specified folder. Share to everyone for your reference. Specific as follows:

Method One:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 for 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

13 /p>

+

+

/

/

/

+

+

+

+

25

+

+

$

+

+

/*

Custom Delete functions that 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 have to determine whether the folder is empty,

Because when the folder is empty at the beginning, it's empty when you get here.

}

Else

{

@unlink ($path);

These two places best still have to use @ Shielding warning error, watching the frustration

}

}

$path = ' d:/Technical document-copy ';

The folder to delete

If the PHP file is not ANSI, but UTF-8 mode,

and the folder you want to delete contains kanji characters, you need to transcode before calling the function

$path =iconv (' utf-8 ', ' gb2312 ', $path);

My_del ($path);

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/987106.html www.bkjia.com true http://www.bkjia.com/PHPjc/987106.html techarticle php Recursive removal of the specified folder method summary php Recursive deletion of the specified folder method: The following is the way you introduced the PHP recursive deletion of the specified folder, the example summarizes the two common ...

  • 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.