$path = '/path/to/directory'; if (is_dir($path)) { $files = scandir($path); foreach ($files as $file) { if ($file != '.' && $file != '..') { $filePath = $path . '/' . $file; if (is_dir($filePath)) { deleteDirectory($filePath); } else { unlink($filePath); } } } rmdir($path); }
$path = '/path/to/directory'; $deletedFiles = []; if (is_dir($path)) { $files = scandir($path); foreach ($files as $file) { if ($file != '.' && $file != '..') { $filePath = $path . '/' . $file; if (is_dir($filePath)) { $deletedFiles = array_merge($deletedFiles, deleteDirectory($filePath)); } else { unlink($filePath); $deletedFiles[] = $filePath; } } } rmdir($path); } // $deletedFiles contains an array of all deleted file pathsThe deleteDirectory function is commonly used in web development projects built with PHP. It is a part of the core PHP library and does not require any external package.