Remove all files in the directory by recursive implementation in PHP

Source: Internet
Author: User

(This document is reproduced in:www.klsele.com.cn)

In PHP, all files under the directory are deleted by recursive implementation. , there is a need for friends to refer to the next.

Recently encountered a real problem, you need to clear all the files under the directory and empty the database. Emptying the database is easy, but how do you recursively delete all the files in a directory? So went to the Internet to study the following information, coupled with their own pondering the solution of the problem.

Paste the code First:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php function delFile($dirName){     if ($handle = opendir("$dirName")){         while (($item = readdir($handle))!=false){             if ($item!="." && $item!="..")  {                 if ( is_dir( "$dirName/$item" ) ) {                          delFileUnderDir( "$dirName/$item" );                  } else unlink("$dirName/$item");         }     }     closedir($handle); } ?><?php     delFile(‘/home/sources‘); ?>

  Let's start by explaining several functions:

Opendir (): function opens a directory handle that can be used by Closedir (), Readdir (), and Rewinddir ().

If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide the output of the error by adding "@" before the function name. For example [email protected] Opendir ("image");

Readdir (): Returns the entry in the directory handle opened by the Opendir function, in order to return the file names in the folder, in the order in which they were sorted by the file system.

Id_dir (): detects if the parameter file is a directory, or True if it is returned.

Un_link (): That is, delete the development file.

So the program executes the idea: the function calls the home directory, and then sequentially detects whether each file is a directory, and if it is a directory, recursively calls the function and deletes files that are not directories until all the files are traversed.

This program only implements the deletion of the contents of the folder, and does not delete the folder itself, if you want to achieve this purpose, plus the following code:

RmDir ($dirName);


http://108.187.5.2
http://108.187.5.3
http://108.187.5.4
http://108.187.5.5
http://108.187.5.6
http://108.187.5.7
http://108.187.5.8
http://108.187.5.9
http://108.187.5.10
http://108.187.5.11
http://108.187.5.12
http://108.187.5.13
http://108.187.5.14
http://108.187.5.15
http://108.187.5.16
http://108.187.5.17
http://108.187.5.18
http://108.187.5.19
http://108.187.5.20
http://108.187.5.21
http://108.187.5.22
http://108.187.5.23
http://108.187.5.24
http://108.187.5.25
http://108.187.5.26
http://108.187.5.27
http://108.187.5.28
http://108.187.5.29
http://108.187.5.30
http://108.187.5.31
http://108.187.5.32
http://108.187.5.33
http://108.187.5.34
http://108.187.5.35
http://108.187.5.36
http://108.187.5.37
http://108.187.5.38
http://108.187.5.39
http://108.187.5.40
http://108.187.5.41
http://108.187.5.42
http://108.187.5.43
http://108.187.5.44
http://108.187.5.45
http://108.187.5.46
http://108.187.5.47
http://108.187.5.48
http://108.187.5.49
http://108.187.5.50
http://108.187.5.51
http://108.187.5.52
http://108.187.5.53
http://108.187.5.54
http://108.187.5.55
http://108.187.5.56
http://108.187.5.57
http://108.187.5.58
http://108.187.5.59
http://108.187.5.60
http://108.187.5.61
http://108.187.5.62
http://108.187.5.63
http://108.187.5.64
http://108.187.5.65
http://108.187.5.66
http://108.187.5.67
http://108.187.5.68
http://108.187.5.69
http://108.187.5.70
http://108.187.5.71
http://108.187.5.72
http://108.187.5.73
http://108.187.5.74
http://108.187.5.75
http://108.187.5.76
http://108.187.5.77
http://108.187.5.78
http://108.187.5.79
http://108.187.5.80
http://108.187.5.81
http://108.187.5.82
http://108.187.5.83
http://108.187.5.84
http://108.187.5.85
http://108.187.5.86
http://108.187.5.87
http://108.187.5.88
http://108.187.5.89
http://108.187.5.90
http://108.187.5.91
http://108.187.5.92
http://108.187.5.93
http://108.187.5.94
http://108.187.5.95
http://108.187.5.96
http://108.187.5.97
http://108.187.5.98
http://108.187.5.99
http://108.187.5.100
Http://108.187.5.101
http://108.187.5.102
http://108.187.5.103
http://108.187.5.104
http://108.187.5.105
http://108.187.5.106
http://108.187.5.107
http://108.187.5.108
http://108.187.5.109
http://108.187.5.110
http://108.187.5.111
http://108.187.5.112
http://108.187.5.113
http://108.187.5.114
http://108.187.5.115
http://108.187.5.116
http://108.187.5.117
http://108.187.5.118
http://108.187.5.119
http://108.187.5.120
http://108.187.5.121
http://108.187.5.122
Http://108.187.5.123
http://108.187.5.124
http://108.187.5.125
http://108.187.5.126
http://108.187.5.127
http://108.187.5.128
http://108.187.5.129
http://108.187.5.130
http://108.187.5.131
http://108.187.5.132
http://108.187.5.133
http://108.187.5.134
http://108.187.5.135
http://108.187.5.136
http://108.187.5.137
http://108.187.5.138
http://108.187.5.139
http://108.187.5.140
http://108.187.5.141
http://108.187.5.142
http://108.187.5.143
http://108.187.5.144
http://108.187.5.145
http://108.187.5.146
http://108.187.5.147
http://108.187.5.148
http://108.187.5.149
http://108.187.5.150
http://108.187.5.151
http://108.187.5.152

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.