好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php删除文件夹/目录下所文件(包含目录) - php文件

php删除文件夹/目录下所文件(包含目录)

以前有介绍过一个删除指定目录下的指定文件下面我们来看删除指定目录所的所有文件只删除一级目录不删除下级目录,具体程序例子如下:

public   function  del(){      header( "Content-Type: text/html; charset=UTF-8" );       echo   '点击文件名可以查看:<br>' ;       $dir  = getcwd (). "/html/" ;       //获取某目录下所有文件、目录名(不包括子目录下文件、目录名)        $handler  = opendir( $dir );       while  (( $filename  = readdir( $handler )) !== false) { //务必使用!==,防止目录下出现类似文件名[0]等情况            if  ( $filename  !=  "."  &&  $filename  !=  ".." ) {                   $files [] =  $filename  ;             }         }             closedir ( $handler );          //打印所有文件名   foreach  ( $files   as   $value ) {       $url  =  'http://' . $_SERVER [ 'HTTP_HOST' ]. "/html/" . $value ;       echo   "<a href='" . $url . "' target='_blank'>" . $value . "</a> | <a href='/index.php?s=/Index1/dodel/name/" . $value . "' target='_self'>删除</a><br />" ;  }           }  public   function  dodel(){      header( "Content-Type: text/html; charset=UTF-8" );       $fname  =  $this ->_get( "name" );       $fname  =  getcwd (). "/html/" . $fname . ".html" ;       if (unlink( $fname )){                      echo   $fname . ' 文件删除成功!<a href="javascript:history.go(-1);">返回</a>' ;        } else {           echo   $fname . ' 删除失败!<a href="javascript:history.go(-1);">返回</a>' ;      }  } 

获取目录下所有文件,包括子目录,代码如下:

function  get_allfiles( $path ,& $files ) {       if ( is_dir ( $path )){           $dp  = dir( $path );           while  ( $file  =  $dp  ->read()){               if ( $file  != "."  &&  $file  != ".." ){                  get_allfiles( $path . "/" . $file ,  $files );              }          }           $dp  ->close();      }       if ( is_file ( $path )){           $files [] =   $path ;      }  }        function  get_filenamesbydir( $dir ){       $files  =   array ();      get_allfiles( $dir , $files );       return   $files ;  }        $filenames  = get_filenamesbydir( "static/image/" );  //打印所有文件名,包括路径   foreach  ( $filenames   as   $value ) {       echo   $value . "<br />" ;  }  

php删除文件夹及其文件夹下所有文件,代码如下:

function  deldir( $dir ) {     //先删除目录下的文件:      $dh =opendir( $dir );     while  ( $file =readdir( $dh )) {       if ( $file != "."  &&  $file != ".." ) {         $fullpath = $dir . "/" . $file ;         if (! is_dir ( $fullpath )) {            unlink( $fullpath );        }  else  {            deldir( $fullpath );        }      }    }         closedir ( $dh );     //删除当前文件夹:      if ( rmdir ( $dir )) {       return  true;    }  else  {       return  false;    }  } 

查看更多关于php删除文件夹/目录下所文件(包含目录) - php文件的详细内容...

  阅读:75次