디렉토리, 내용 및 모든 하위 디렉토리와 내용을 삭제하도록 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 감사합니다, 거 너무 많이, WOW ... 지금 – Terry