我有這段代碼遞歸刪除文件和目錄。它工作正常,但有一點問題。如果$ 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