Home > Backend Development > PHP Tutorial > Summary of how to recursively delete specified folders in php_PHP tutorial

Summary of how to recursively delete specified folders in php_PHP tutorial

WBOY
Release: 2016-07-13 09:56:38
Original
1081 people have browsed it

Summary of how to recursively delete a specified folder in php

How to recursively delete a specified folder in php:

The following introduces you to the method of recursively deleting specified folders in PHP. The example summarizes two commonly used techniques for recursively deleting folders. It is of great practical value. Friends who need it can refer to it

This example summarizes two methods of recursively deleting specified folders in PHP. Share it with everyone for your reference. The details are as follows:

Method 1:

 ?

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

recursiveDelete($dir . '/' . $file);

}

else

{

unlink($dir . '/' . $file); // remove this file

}

}

@closedir($handle);

rmdir ($dir);

}

}

1

2

3

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

/*

自定义的删除函数,可以删除文件和递归删除文件夹

*/

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

//这种方法不用判断文件夹是否为空,

//因为不管开始时文件夹是否为空,到达这里的时候,都是空的

}

else

{

@unlink($path);

//这两个地方最好还是要用@屏蔽一下warning错误,看着闹心

}

}

$path='d:/技术文档 - 副本';

//要删除的文件夹

//如果php文件不是ANSI,而是UTF-8模式,

//而且要删除的文件夹中包含汉字字符的话,调用函数前需要转码

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

my_del($path);

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 for this directory recursiveDelete($dir . '/' . $file); } else { unlink($dir . '/' . $file); // remove this file } } @closedir($handle); rmdir ($dir); } }
Method 2:  ?
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 /* Customized 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 determine whether the folder is empty, //Because no matter whether the folder is empty at the beginning, it will be empty when you get here } else { @unlink($path); //It’s best to use @ to block warning errors in these two places, which will look annoying } } $path='d:/Technical Documentation - Copy'; //Folder to be deleted //If the php file is not ANSI, but UTF-8 mode, //If the folder to be deleted contains Chinese characters, it needs to be transcoded before calling the function //$path=iconv( 'utf-8', 'gb2312',$path ); my_del($path);

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987106.htmlTechArticleHow to delete a specified folder recursively in php Summary of how to delete a specified folder recursively in php: The following is an introduction to php for you How to recursively delete specified folders. The example summarizes two commonly used recursive methods...
Related labels:
php
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