以前有介绍过一个删除指定目录下的指定文件下面我们来看删除指定目录所的所有文件只删除一级目录不删除下级目录,具体程序例子如下:
- public function del(){
- header("Content-Type: text/html; charset=UTF-8");
- echo '点击文件名可以查看:<br>';
- $dir =getcwd()."/html/";
-
- $handler = opendir($dir);
- while (($filename = readdir($handler)) !== false) {
- 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;
- }
- }
|