How to delete all the folders inside a parent folder with PHP?
I have tried this, but it isn't working:
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 this :
function del($dir)
{
foreach(glob($dir . '/*') as $file)
{
if(is_dir($file))
del($file);
}
rmdir($dir);
}
It will also delete nested folders
Related
I want to delete files from multiple direcories in PHP. The problem is when the code is run it deletes everything. I have a main folder which has a lots of folders in it. Those folders have files that i want to delete except that file in $filesToKeep variable. I'm a beginner PHP developer, and i really don't know how could i find the problame. If there is an another easier way to delete those files could be helpful too.
Here is my code:
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);
}
}
$filesToKeep = array(
'partner-profil-480.jpg'
);
$dirList = glob('*');
foreach ($dirList as $file) {
if (!in_array($file, $filesToKeep)) {
if (is_dir($file)) {
rrmdir($file);
} else {
unlink($file);
}//END IF
}//END IF
}//END FOREACH LOOP
?>
I found the solution a while ago just i forgot to post it.
Here it is:
$path = "yourpath";
function dirToArray($dir) {
$filesToKeep = array(
'your-file-to-keep'
);
$contents = array();
foreach (scandir($dir) as $node) {
if ($node == '.' || $node == '..') continue;
if (is_dir($dir . '/' . $node)) {
$contents[$node] = dirToArray($dir . '/' . $node);
} else {
$contents[] = $node;
print_r(json_encode($node, JSON_PRETTY_PRINT));
if(!in_array($node, $filesToKeep)){
unlink($dir . '/' . $node);
}
}
rmdir($dir . '/' . $node);
}
}
$r = dirToArray($path);
print_r($r);
I have a 'cache' folder on my webserver where .html files are stored, the file structure is as follows:
cache > user#gmail.com > several .html files.
I need a PHP script that will browse all the subdirectors within the user folders and delete files older than 3 months.
I have this script so far:
$DIR = '/cache/';
if ($handle = opendir($DIR)) {
while (false !== ($file = readdir($handle))) {
if ( filemtime($DIR.$file) <= time()-60*60*24*120 ) { //120 days?
unlink($DIR.$file);
}
}
closedir($handle);
}
But it does not handle sub-directories. It is also giving me errors because it is trying to unlink directorys..
Update: trying to add a count feature:
<?php
rrmdir(/www/deletecontentsofthisfolder/);
echo $count . ' files deleted!';
function rrmdir($dir,$count=0)
{
if (is_dir($dir))
{
$objects = scandir($dir);
foreach ($objects as $object)
{
if ($object != "." && $object != "..")
{
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object,$count);
if (filemtime($dir."/".$object) <= time()-60*60*24*120) #unlink($dir."/".$object); count++
}
}
reset($objects);
//rmdir($dir);
}
return $count;
}
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);
if (filemtime($dir."/".$object) <= time()-60*60*24*120) #unlink($dir."/".$object);
}
}
reset($objects);
//rmdir($dir);
}
}
This question already has answers here:
Delete directory with files in it?
(34 answers)
Closed 5 years ago.
I need a script which can remove a whole directory with all their subfolders, files and etc. I tried with this function which I found in internet before few months ago but it not work completely.
function deleteFile($dir) {
if(substr($dir, strlen($dir)-1, 1) != '/') {
$dir .= '/';
}
if($handle = opendir($dir)) {
while($obj = readdir($handle)) {
if($obj != '.' && $obj != '..') {
if(is_dir($dir.$obj)) {
if(!deleteFile($dir.$obj)) {
echo $dir.$obj."<br />";
return false;
}
}
elseif(is_file($dir.$obj)) {
if(!unlink($dir.$obj)) {
echo $dir.$obj."<br />";
return false;
}
}
}
}
closedir($handle);
if(!#rmdir($dir)) {
echo $dir.'<br />';
return false;
}
return true;
}
return true;
}
For the test I use a unpacked archive of prestashop and I try to delete the folder where archive is unpacked but it doesn't work.
/home/***/public_html/prestashop/img/p/3/
/home/***/public_html/prestashop/img/p/3
/home/***/public_html/prestashop/img/p
/home/***/public_html/prestashop/img
These are the problem folders. At the first time I think - "May is a problem with the chmod of the files" but when I test with all files chmod permission 755 (after that with 777) - the result was the same.
<?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
function delete_files($dir) {
foreach(glob($dir . '/*') as $file) {
if(is_dir($file)) delete_files($file); else unlink($file);
} rmdir($dir);
}
You can use a cleaner method for doing a recursive delete of a directory.
Example:
function recursiveRemove($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)) recursiveRemove($file);
elseif (is_file($file)) unlink($file);
}
}
rmdir($dir);
}
Usage:
recursiveRemove("test/dir/");
The easiest and the best way using the system() method
$dir = dirname ( "/log" );
if ($handle = opendir($dir)) {
while (( $file = readdir($handle)) !== false ) {
if ($file != "." && $file != "..") {
system("rm -rf ".escapeshellarg($dir.'/'.$file));
}
}
}
closedir($handle);
/**
* Deletes a directory and all files and folders under it
* #return Null
* #param $dir String Directory Path
*/
function rmdir_files($dir) {
$dh = opendir($dir);
if ($dh) {
while($file = readdir($dh)) {
if (!in_array($file, array('.', '..'))) {
if (is_file($dir.$file)) {
unlink($dir.$file);
}
else if (is_dir($dir.$file)) {
rmdir_files($dir.$file);
}
}
}
rmdir($dir);
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to delete a folder with contents using PHP
I know that you can remove a folder that is empty with rmdir. And I know you can clear a folder with the following three lines.
foreach($directory_path as $file) {
unlink($file);
}
But what if one of the files is actually a sub directory. How would one get rid of that but in an infinite amount like the dual mirror effect. Is there any force delete directory in php?
Thanks
This function will delete a directory recursively:
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
This one too:
function rmdir_recursive($dir) {
$it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach($it as $file) {
if ($file->isDir()) rmdir($file->getPathname());
else unlink($file->getPathname());
}
rmdir($dir);
}
From PHP rmdir page:
<?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);
}
}
?>
And
<?php
function delTree($dir) {
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
delTree( $file );
else
unlink( $file );
}
if (is_dir($dir)) rmdir( $dir );
}
?>
<?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);
}
}
?>
from PHP's documentation
how can i delete multiple files from the folder?
code:$query=$this->main_model->get($id);
if($query->num_rows()>0)
{
foreach ($query->result() as $row)
{
unlink("uploads/".$id."/".$row->img_name);
unlink("uploads/".$id."/".$row->img_name_tn);
unlink("uploads/".$id."/".$row->file);
unlink("uploads/".$id."/".$row->file2);
unlink("uploads/".$id."/Thumbs.db");
}
rmdir("uploads/".$id);
}
here is the code i used but it delete the files at ones. and how can i delete all files from the folder?
originally from php.net:
<?php
$dir = '/uploads/';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") { // strip the current and previous directory items
unlink($dir . $file); // you can add some filters here, aswell, to filter datatypes, file, prefixes, suffixes, etc
}
}
closedir($handle);
}
?>
I found this at php.net:
"The shortest recursive delete possible"
function rrmdir($path) {
return is_file($path)?
#unlink($path):
array_map('rrmdir',glob($path.'/*'))==#rmdir($path)
;
}
You could do it like this:
function delete_files($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
}
}
closedir($dir_handle);
return true;
}
You need to use a recursive function. A comment from the rmdir page have written a function on how to do it, see http://www.php.net/manual/en/function.rmdir.php#98622. This code will delete the folder and everything in it.
<?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);
}
}
?>