php 用php删除所有文件、文件夹及其子文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11613840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:52:26  来源:igfitidea点击:

Remove all files, folders and their subfolders with php

phpfile-iodirectorysubdirectory

提问by donald123

I need a script which can remove a whole directory with all their subfolders, files and etc. I tried with this function which I found in internet before few months ago but it not work completely.

我需要一个脚本,它可以删除整个目录及其所有子文件夹、文件等。我尝试过几个月前在互联网上找到的这个功能,但它不能完全工作。

function deleteFile($dir) {
    if(substr($dir, strlen($dir)-1, 1) != '/') { 
        $dir .= '/'; 
    }
    if($handle = opendir($dir)) { 
        while($obj = readdir($handle)) { 
            if($obj != '.' && $obj != '..') { 
                if(is_dir($dir.$obj)) { 
                    if(!deleteFile($dir.$obj)) {
                        echo $dir.$obj."<br />";
                        return false;
                    }
                }
                elseif(is_file($dir.$obj)) { 
                    if(!unlink($dir.$obj)) {
                        echo $dir.$obj."<br />";
                        return false;
                    }
                }
            }
        }
        closedir($handle); 
        if(!@rmdir($dir)) {
            echo $dir.'<br />';
            return false;
        }
        return true;
    }
    return true;
}

For the test I use a unpacked archive of prestashop and I try to delete the folder where archive is unpacked but it doesn't work.

对于测试,我使用了 prestashop 的解压存档,我尝试删除解压存档的文件夹,但它不起作用。

/home/***/public_html/prestashop/img/p/3/
/home/***/public_html/prestashop/img/p/3
/home/***/public_html/prestashop/img/p
/home/***/public_html/prestashop/img

These are the problem folders. At the first time I think - "May is a problem with the chmod of the files" but when I test with all files chmod permission 755 (after that with 777) - the result was the same.

这些是问题文件夹。我第一次想 - “可能是文件的 chmod 有问题”但是当我使用 chmod 权限 755(之后是 777)测试所有文件时 - 结果是一样的。

回答by donald123

<?php
  function rrmdir($dir) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if (filetype($dir."/".$object) == "dir") 
           rrmdir($dir."/".$object); 
        else unlink   ($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
 }
?>

Try out the above code from php.net

php.net尝试上面的代码

Worked fine for me

对我来说很好用

回答by Gaurang P

function delete_files($dir) { 
  foreach(glob($dir . '/*') as $file) { 
    if(is_dir($file)) delete_files($file); else unlink($file); 
  } rmdir($dir); 
}

回答by Addramyr

You can use a cleaner method for doing a recursive delete of a directory.

您可以使用更清洁的方法对目录进行递归删除。

Example:

例子:

function recursiveRemove($dir) {
    $structure = glob(rtrim($dir, "/").'/*');
    if (is_array($structure)) {
        foreach($structure as $file) {
            if (is_dir($file)) recursiveRemove($file);
            elseif (is_file($file)) unlink($file);
        }
    }
    rmdir($dir);
}

Usage:

用法:

recursiveRemove("test/dir/");

回答by Ale_

The easiest and the best way using the system()method

使用system()方法的最简单和最好的方法

$dir = dirname ( "/log" );
if ($handle = opendir($dir)) {
    while (( $file = readdir($handle)) !== false ) {
        if ($file != "." && $file != "..") {
            system("rm -rf ".escapeshellarg($dir.'/'.$file));
        }
    }
}
closedir($handle);

回答by jit

/**
 * Deletes a directory and all files and folders under it
 * @return Null
 * @param $dir String Directory Path
 */
function rmdir_files($dir) {
 $dh = opendir($dir);
 if ($dh) {
  while($file = readdir($dh)) {
   if (!in_array($file, array('.', '..'))) {
    if (is_file($dir.$file)) {
     unlink($dir.$file);
    }
    else if (is_dir($dir.$file)) {
     rmdir_files($dir.$file);
    }
   }
  }
  rmdir($dir);
 }
}