Home > Backend Development > PHP Tutorial > How to delete a specified directory in php_PHP tutorial

How to delete a specified directory in php_PHP tutorial

WBOY
Release: 2016-07-13 09:58:10
Original
766 people have browsed it

How to delete the specified directory in php

This article describes the method of deleting the specified directory in php. Share it with everyone for your reference. The specific analysis is as follows:

This code can realize the function of recursively deleting subdirectories

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

32

33

34

/**

* Delete a file, or a folder and its contents

* (recursive algorithm)

* @author Aidan Lister

* @version 1.0.3

* @param string $dirname Directory to delete

* @return bool Returns TRUE on success, FALSE on failure

*/

function rmdirr($dirname)

{

// Sanity check

if (!file_exists($dirname)) {

return false;

}

// Simple delete for a file

if (is_file($dirname) || is_link($dirname)) {

return unlink($dirname);

}

// Loop through the folder

$dir = dir($dirname);

while (false !== $entry = $dir->read()) {

// Skip pointers

if ($entry == '.' || $entry == '..') {

continue;

}

// Recurse

rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);

}

// Clean up

$dir->close();

return rmdir($dirname);

}

?>

1 2 3

4

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 32 33 34
<🎜>/**<🎜> <🎜>* Delete a file, or a folder and its contents<🎜> <🎜>* (recursive algorithm)<🎜> <🎜>* @author Aidan Lister * @version 1.0.3 * @param string $dirname Directory to delete * @return bool Returns TRUE on success, FALSE on failure */ function rmdirr($dirname) { // Sanity check if (!file_exists($dirname)) { return false; } // Simple delete for a file if (is_file($dirname) || is_link($dirname)) { return unlink($dirname); } // Loop through the folder $dir = dir($dirname); while (false !== $entry = $dir->read()) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Recurse rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); } // Clean up $dir->close(); return rmdir($dirname); } ?>
http://www.bkjia.com/PHPjc/979231.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/979231.htmlTechArticleHow to delete a specified directory in php. This article describes how to delete a specified directory in php. Share it with everyone for your reference. The specific analysis is as follows: This code can recursively delete subdirectories...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template