-
- /**
- * Loop delete directory and file function
- * @delDirAndFile
- * @param $dirName
- * @edit bbs.it-home.org
- function delDirAndFile( $dirName )
- {
- if ( $handle = opendir( "$dirName" ) ) {
- while ( false !== ( $item = readdir( $handle ) ) ) {
- if ( $item != "." && $item != ".." ) {
- if ( is_dir( "$ dirName/$item" ) ) {
- delDirAndFile( "$dirName/$item" );
- } else {
- if( unlink( "$dirName/$item" ) ) echo "File deleted successfully: $dirName/$item
- n";
- }
- }
- }
- closedir( $handle );
- if( rmdir( $dirName ) ) echo "Directory deleted successfully: $dirName
- n";
- }
- }
- ?>
Copy code
Function 2, only deletes files in the specified directory, not the directory folder.
-
- /**
- * Loop all files in the directory
- * @func delFileUnderDir
- * @param $dirName
- * @edit bbs.it-home.org
- */
- function delFileUnderDir( $dirName )
- {
- if ( $handle = opendir( "$dirName" ) ) {
- while ( false !== ( $item = readdir( $handle ) ) ) {
- if ( $item != "." && $item != ".." ) {
- if ( is_dir( "$dirName/$item" ) ) {
- delFileUnderDir( "$dirName/$item" );
- } else {
- if( unlink( "$dirName/$item" ) ) echo "File deleted successfully: $dirName/$item
- n";
- }
- }
- }
- closedir ( $handle );
- }
- }
- ?>
Copy the code
Let’s look at the specific calling example.
1. Delete a sibling directory named "upload", that is, all files in this directory:
-
- delDirAndFile( 'upload');
- ?>
-
Copy code
2, delete all files in a directory named "upload" (but there is no need to delete the directory file folder):
-
- delFileUnderDir( 'upload');
- ?>
Copy code
|