Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP: Unlink All Files Within A Directory, and then Deleting That Directory
Use glob to find all files matching a pattern.
function recursive_directory_removal($directory) {
foreach(glob("{$directory}/*") as $file) {
if(is_dir($file)) {
recursive_directory_removal($file);
} else {
unlink($file);
}
}
rmdir($directory);
}
On PHP version 5.3 and above, the following code can be used −
$dir = ...
array_walk(glob($dir . '/*'), function ($fn) {
if (is_file($fn))
unlink($fn);
});
unlink($dir);Advertisements