특정 디렉토리에 모든 파일 삭제 또는 특정 디렉토리에 특정 확장자 파일만 삭제

<?php
// 특정디렉토리에 모든 파일 삭제
$directory = "/www/upload";
$handle = opendir($directory); // 절대경로
while ($file = readdir($handle)) {
@unlink($directory.$file);
}
closedir($handle);
?>
<?php
// 특정 디렉토리에 특정 확장자만 삭제
$directory = "/www/upload";
$handle = opendir($directory); // 절대경로
while ($file = readdir($handle)) {
$fileInfo = pathinfo($file);
$fileExt = $fileInfo['extension']; // 파일의 확장자를 구함
if($fileExt == "jpg") {
@unlink($directory.$file);
}
}
closedir($handle);
?>