如何删除目录下的所有文件(保留最上阶目录)
我想删除一个目录下的目录及文件,但保留最上阶目录. 如何说, 要删除/upload/下的所有目录及文件,但保留/upload目录.
下面函数会把/upload目录也删除
PHP code1 | <!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:
|
로그인 후 복사
------解决方案--------------------注释上有写:
PHP code
PHP code
PHP code1 | function delDir( $dir ) { $t = array (); $dh = opendir( $dir ); while ( $file =readdir( $dh )) { if ( $file != "." && $file != ".." ) { $fullpath = $dir . "/" . $file ; if (! is_dir ( $fullpath )) { unlink( $fullpath ); } else { delDir( $fullpath ); $t [] = $fullpath ;
|
로그인 후 복사
PHP code1 2 3 4 5 6 | function delDir( $dir ) { $t = array (); $r = true; $dh = opendir( $dir ); while ( $file =readdir( $dh )) { if ( $file != "." && $file != ".." ) { $fullpath = $dir . "/" . $file ; if (! is_dir ( $fullpath )) { unlink( $fullpath ); } else { $r &= delDir( $fullpath ); $t [] = $fullpath ;
</div>
|
로그인 후 복사