How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?
The user-contributed section in the manual page of rmdir
contains a decent implementation:
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object))
rrmdir($dir. DIRECTORY_SEPARATOR .$object);
else
unlink($dir. DIRECTORY_SEPARATOR .$object);
}
}
rmdir($dir);
}
}
In PHP how do I recursively remove all folders that aren't empty?
From the first comment in the official documentation.
http://php.net/manual/en/function.rmdir.php
<?php
// When the directory is not empty:
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
?>
Edited rmdir to rrmdir to correct typo from obvious intent to create recursive function.
Delete directory with files in it?
There are at least two options available nowadays.
Before deleting the folder, delete all its files and folders (and this means recursion!). Here is an example:
public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}And if you are using 5.2+ you can use a RecursiveIterator to do it without implementing the recursion yourself:
$dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
Remove all files, folders and their subfolders with php
<?php
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir")
rrmdir($dir."/".$object);
else unlink ($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
?>
Try out the above code from php.net
Worked fine for me
List all the files and folders in a Directory with PHP recursive function
Get all the files and folders in a directory, don't call function when you have .
or ..
.
Your code :
<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
var_dump(getDirContents('/xampp/htdocs/WORK'));
Output (example) :
array (size=12)
0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
1 => string '/xampp/htdocs/WORK/index.html' (length=29)
2 => string '/xampp/htdocs/WORK/js' (length=21)
3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
11 => string '/xampp/htdocs/WORK/styles.less' (length=30)
Deleting a directory with all of the files inside it
This will do it, just be careful you don't delete the whole server:
$dir='your/directory';
exec('rm -rf '.escapeshellarg($dir));
PHP Delete all files that are empty in a Directory and any sub directories
empty($file)
doesn't check that the file referenced by $file
is empty, it checks that the variable $file
is empty. I.e., if $file
contains an empty string ""
or null
then empty($file)
will return true. Since your $file
contains a non-empty string (the name of the file), empty($file)
will always return false, regardless of the file contents. You want to check that filesize($file)
is zero.
Related Topics
How to Remove Line Breaks (No Characters!) from the String
Remove All Attributes from HTML Tags
Using an Array as Needles in Strpos
Using Sessions & Session Variables in a PHP Login Script
Create Array Tree from Array List
What Does It Mean to Start a PHP Function With an Ampersand
Select Count(*) from Table of MySQL in PHP
Check File Extension in Upload Form in PHP
Woocommerce Action Hooks and Overriding Templates
PHP Function to Get the Subdomain of a Url
How Unique Is the PHP Session Id
Keeping Session Alive With Curl and PHP
How Safe Are PHP Session Variables
Encrypt and Decrypt Text With Rsa in PHP