我有这段代码递归删除文件和目录。它工作正常,但有一点问题。如果$ path =/var/www/foo /它会删除foo内的所有内容,但不包含foo。我也想删除foo目录。任何想法?递归删除
public function delete($path) {
if(!file_exists($path)) {
throw new RecursiveDirectoryException('Directory doesn\'t exist.');
}
$directoryIterator = new DirectoryIterator($path);
foreach($directoryIterator as $fileInfo) {
$filePath = $fileInfo->getPathname();
if(!$fileInfo->isDot()) {
if($fileInfo->isFile()) {
unlink($filePath);
}
else if($fileInfo->isDir()) {
if($this->emptyDirectory($filePath)) {
rmdir($filePath);
}
else {
$this->delete($filePath);
rmdir($filePath);
}
}
}
}
}
@stereofrog:几乎所有的东西? – Bobby 2010-12-21 08:02:00