PHP methods for deleting all directories and files in a specified directory _php tutorial

Source: Internet
Author: User

How PHP deletes all directories and files in the specified directory


This article mainly introduces the method that PHP deletes all directories and files in the specified directory, and realizes the function of recursive deletion through the custom function Delfile, which has a certain reference value, the friends who need can refer to the following

This example describes how PHP deletes all directories and files in the specified directory. Share to everyone for your reference. The implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/*

*

* Delete all directories and files in the specified directory (or specify files)

* Expandable Add some options (such as whether to delete the original directory, etc.)

* Use caution when deleting file sensitive operations

* @param $dir directory path

* @param array $file _type specified file type

*/

function Delfile ($dir, $file _type= ") {

if (Is_dir ($dir)) {

$files = Scandir ($dir);

Open the directory//list all files in the directory and remove them. And..

foreach ($files as $filename) {

if ($filename! = '. ' && $filename! = ' ... ') {

if (!is_dir ($dir. '/'. $filename)) {

if (Empty ($file _type)) {

Unlink ($dir. '/'. $filename);

}else{

if (Is_array ($file _type)) {

Regular matches the specified file

if (Preg_match ($file _type[0], $filename)) {

Unlink ($dir. '/'. $filename);

}

}else{

Specify a file that contains some strings

if (False!=stristr ($filename, $file _type)) {

Unlink ($dir. '/'. $filename);

}

}

}

}else{

Delfile ($dir. '/'. $filename);

RmDir ($dir. '/'. $filename);

}

}

}

}else{

if (file_exists ($dir)) unlink ($dir);

}

}

Delfile (DirName (__file__), ' HTML ');

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

http://www.bkjia.com/PHPjc/961079.html www.bkjia.com true http://www.bkjia.com/PHPjc/961079.html techarticle how PHP deletes all directories and files in the specified directory this article mainly introduces the method of removing all directories and files in the specified directory by PHP, and realizes recursion by custom function Delfile ...

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