
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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);
- Related Articles
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- Recursively List All Files in a Directory Including Symlinks
- How to delete all files in a directory with Python?
- How to list all files in a directory using Java?
- Java program to List all files in a directory recursively
- How to unzip all zipped files in a Linux directory?
- Golang program to get all files present in a directory
- C# Program to Get all Files Present in a Directory
- Move All Files Including Hidden Files Into Parent Directory in Linux
- C Program to list all files and sub-directories in a directory
- Java program to delete all the files in a directory recursively (only files)
- Get all subdirectories of a given directory in PHP
- How do I list all files of a directory in Python?
- How to list all files (only) from a directory using Java?
- How to perform grep operation on all files in a directory?

Advertisements