Method 1:
Copy codeThe Code is as follows: <? Php
/**
* Directory generation class: UtilsMakeDir
* @ Author yepeng
* @ Since 2010.3.18
*/
Class UtilsMakeDir {
// This directory is not created when the base directory is created. This should be an existing Directory.
Private static $ makeBasePath = 'video ';
Private static $ delBasePath = 'video ';
/**
* Recursively create a directory,
* If the creation succeeds, the full path is returned,
* If creation fails, false is returned.
* @ Param String $ pathString path String, for example, '2014/1/123'
* @ Return false or string
Public static function makeDir ($ pathString ){
$ PathArray = explode ('/', $ pathString );
If (empty ($ pathArray [0]) {
Return false;
}
$ Path = array_shift ($ pathArray );
Self: $ basePath = self: $ basePath. '/'. $ path;
If (is_dir (self: $ basePath )){
$ Path = implode ('/', $ pathArray );
Self: makeDir ($ path );
}
Else {
@ Mkdir (self: $ basePath, 0777 );
$ Path = implode ('/', $ pathArray );
Self: makeDir ($ path );
}
If (is_dir (self: $ basePath )){
Return self: $ basePath;
}
Else {
Return false;
}
}*/
/**
* Create a directory, including the base Directory, which should be placed under video (where video exists). The input parameter should be video/2/3/4.
* If the creation succeeds, the full path is returned,
* If creation fails, false is returned.
* @ Param String $ pathString path String, for example, 'video/2/3/123'
* @ Return false or string
**/
Public static function makeDir ($ pathString ){
$ PathArray = explode ('/', $ pathString );
$ TmpPath = array_shift ($ pathArray );
Foreach ($ pathArray as $ val ){
$ TmpPath. = "/". $ val;
If (is_dir ($ tmpPath )){
Continue;
}
Else {
@ Mkdir ($ tmpPath, 0777 );
}
}
If (is_dir ($ tmpPath )){
Return $ tmpPath;
}
Else {
Return false;
}
}/**
* Recursive Deletion
* Delete directories and files
* If you upload a path like 'video/2/3/4', all directories and files under '4' will be deleted.
* @ Param string $ stringPath
*/
Public static function delDir ($ stringPath ){
If (! $ Handle = @ opendir ($ stringPath )){
Return false;
}
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.' & $ File! = '..'){
$ Tmpdir = $ stringPath. "/". $ file;
If (is_dir ($ tmpdir )){
Self: delDir ($ tmpdir );
Rmdir ($ tmpdir );
}
If (is_file ($ tmpdir )){
Unlink ($ tmpdir );
}
}
}
Closedir ($ handle );
}}
?>
Loop + recursion: The test is successful in winxp. As long as the PHP file is encoded as gb2312 and the file name is random, change the file name to gb2312.Copy codeThe Code is as follows: <? Php
Deltree ('./duplicate, duplicate, and duplicate, aaa ');
Function deltree ($ pathdir)
{
// Echo $ pathdir. '<br/>'; //
If (is_empty_dir ($ pathdir) // if it is empty
{
Rmdir ($ pathdir); // Delete directly
}
Else
{// Otherwise, read this directory, except
$ D = dir ($ pathdir );
While ($ a = $ d-> read () // only delete under $ pathdir
{
If (is_file ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..'))
{
Unlink ($ pathdir. '/'. $ a); // delete a file directly.
} Elseif (is_dir ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..') // If it is a directory
{
If (! Is_empty_dir ($ pathdir. '/'. $ a) // whether it is null
{
Deltree ($ pathdir. '/'. $ a); // If not, call
} Else
{
Rmdir ($ pathdir. '/'. $ a); // delete it if it is null.
}
}
}
$ D-> close ();
// Echo "all files in the directory must be deleted first"; //
Rmdir ($ pathdir );
}
}
Function is_empty_dir ($ pathdir)
{
// Check whether the directory is empty. Isn't my method good? Anything except... is not empty
$ D = opendir ($ pathdir );
$ I = 0;
While ($ a = readdir ($ d ))
{
$ I ++;
}
Closedir ($ d );
If ($ I> 2) {return false ;}
Else return true;
}
?>
The second recursion method is successfully tested in winxp. As long as the PHP file is encoded as gb2312 and the file name is random, the file name should be changed to gb2312.Copy codeThe Code is as follows: <? Php
Header ("Content-Type: text/html; charset = gb2312 ");
If (deleteDir ('./ aaa (aaa '))
Echo "deleted successfully ";
Function deleteDir ($ dir)
{
If (@ rmdir ($ dir) = false & is_dir ($ dir) // You cannot delete all files.
{
If ($ dp = opendir ($ dir ))
{
While ($ file = readdir ($ dp ))! = False)
{
If ($ file! = '.' & $ File! = '..')
{// Echo $ file = $ dir. '/'. $ file; echo '<br/> ';
$ File = $ dir. '/'. $ file;
If (is_dir ($ file) // is a real directory
{
DeleteDir ($ file );
} Else {
Unlink ($ file );
}
}
}
Closedir ($ dp );
} Else
{
Return false;
}
}
If (is_dir ($ dir) & @ rmdir ($ dir) = false) // The directory cannot be deleted.
Return false;
Return true;
}
?>
The third recursive method is successfully tested in winxp, which is useful for listing directory files.Copy codeThe Code is as follows: <? Php
Function listDir ($ dir)
{
Static $ break = 0; if ($ break ++ = 100) exit; // controls Deep Layers
Static $ I =-0;
If (is_dir ($ dir) // directory
{
If ($ dh = opendir ($ dir) // open
{
While ($ file = readdir ($ dh ))! = False)
{
If (is_dir ($ dir. "/". $ file) & $ file! = "." & $ File! = "..") // Directory
{
$ J = $ I; while ($ j --) echo "-------";
Echo "<B> <font color = 'red'> directory name: </font> </B> ". $ dir. "/". $ file. "<br> $ I ++;
ListDir ($ dir. "/". $ file );
$ I --;
}
Else
{
If ($ file! = "." & $ File! = "..")
{
$ J = $ I; while ($ j --) echo "-------";
$ Ext = trim (extend ($ file ));
// If ($ ext = 'jpg ')
Echo $ dir. '/'. $ file. "<br> ";
}
}
}
Closedir ($ dh );
}
}
}
Function extend ($ file_name)
{
$ Retval = "";
$ Pt = strrpos ($ file_name ,".");
If ($ pt) $ retval = substr ($ file_name, $ pt + 1, strlen ($ file_name)-$ pt );
Return ($ retval );
}
// Start running
ListDir (".");
?>