我已经设置了一些PHP来删除一个目录,它的内容,以及任何子目录及其内容...我是PHP新手,所以我肯定是在做一些错误或者是错误的以最低效的方式做事。删除目录内容和子目录内容
寻找关于如何做到这一点更好......
顺便说一些参考或建议,该代码工作正常。使用PHP 5.3.8。
chmod($main_dir, 0755);
if ($handle = opendir($main_dir)) {
while (false !== ($entry = readdir($handle))) {
$absolute_path = $main_dir.'/'.$entry;
if ($entry != "." && $entry != "..") {
chmod($absolute_path, 0755);
unlink($absolute_path);
//check if any folders exist, then delete files within
if (file_exists($absolute_path) && is_dir($absolute_path)) {
if ($child_handle = opendir($absolute_path)) {
while (false !== ($child_entry = readdir($child_handle))) {
$child_absolute_path = $absolute_path.'/'.$child_entry;
if ($child_entry != "." && $child_entry != "..") {
chmod($child_absolute_path, 0755);
unlink($child_absolute_path);
}
}
closedir($child_handle);
}
}
rmdir($absolute_path);
}
}
closedir($handle);
}
rmdir($main_dir);
有什么想法?非常感激! 即时通讯使用PHP 5.3.8
这可能是在[代码审查]更合适(http://codereview.stackexchange.com/?as=1) – noel
@shakabra谢谢,现在要去看看...... – Terry