Jump to content

Recursive Delete Function


dsjoes

Recommended Posts

i have the below script but it doesn't work it just refreshes the page anyone know what is wrong

 

<?php
$path = "../../gallery/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	

$dirname = $path; 

//Recursive Delete Function

function DELETE_RECURSIVE_DIRS($dirname)
{ // recursive function to delete
  // all subdirectories and contents: 
  if(is_dir($dirname))$dir_handle=opendir($dirname); 
  while($file=readdir($dir_handle)) 
  {                                                      
    if($file!="." && $file!="..") 
    { 
      if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); 
      else DELETE_RECURSIVE_DIRS($dirname."/".$file); 
    } 
  } 
  closedir($dir_handle); 
  rmdir($dirname); 
  return true; 
}

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/230997-recursive-delete-function/
Share on other sites

this would be better, looks like that function is only needed once, so you minus well take it out.

<?php
$path = "../../gallery/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	

$dirname = $path; 

	if(is_dir($dirname))$dir_handle=opendir($dirname); 
	while($file=readdir($dir_handle)) 
	{                                                      
		if($file!="." && $file!="..") 
		{ 
			if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); 
			else  rmdir($dirname); 
		}
	}
}
}
?>

after this

if(is_dir($dirname))$dir_handle=opendir($dirname);

add

?><pre><?php print_r($dir_handle); ?></pre><?php

 

and post what you get, thanks.

The issue now, seems to be coming from $dir_handle not being read properly. We need to check if opendir was a success, so we need to dump $dir_handle.

That means opendir is not getting the path. We may have an issue with is_dir checking the path.

What i'd remove $dirname = $path; and replace $dirname with path everywhere else.

Try this one out.

<?php
$path = "../../gallery/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{

if(is_dir($path)) {
	$dir_handle=opendir($path); 
} else {
	die('directory not found.');
}
while($file=readdir($dir_handle)) 
{                                                      
	if($file!="." && $file!="..") 
	{ 
		if(!is_dir($path."/".$file))
		{
		 // we unlink the file the remove the directory
		unlink ($path."/".$file); 
		rmdir($path); 
		}
	}
}
}
?>

I can't do everything for you as this is not my script. So if the directory is not found, what do you think you should do next?

I personally think you should check $path and make sure it has the correct directory, i don't know your dir info, so I can't make this next part for you. It's up to you to set the proper directory and to get the script to move past that part, once you've done that and if you're still having issues feel free to reply.

fixed it but now i get a load of unlink rmdir errors

Warning: unlink(../../gallery/gallery_files/gallery/Group1/.) [function.unlink]: Is a directory

Warning: rmdir(../../gallery/gallery_files/gallery/Group1) [function.rmdir]: Directory not empty

Warning: unlink(../../gallery/gallery_files/gallery/Group1/..) [function.unlink]: Is a directory

Warning: unlink(../../gallery/gallery_files/gallery/Group1/thumbnails) [function.unlink]: Is a directory

Let's just try making your first code work.

I'm confused about the $file != . && $file != .., so we'll just make your initial code call the function properly and we can go from there.

$path = "../../gallery/gallery_files/gallery/";
$dirname = $path; 
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	if (is_dir($dirname))
	{
		DELETE_RECURSIVE_DIRS($dirname);
	}
	else {
		die(' dirname variable is wrong, fix the path.');
	}
}
}
function DELETE_RECURSIVE_DIRS($dirname)
{ // recursive function to delete
  // all subdirectories and contents: 
  if(is_dir($dirname))$dir_handle=opendir($dirname); 
  while($file=readdir($dir_handle)) 
  {                                                      
    if($file!="." && $file!="..") 
    { 
      if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); 
      else DELETE_RECURSIVE_DIRS($dirname."/".$file); 
    } 
  } 
  closedir($dir_handle); 
  rmdir($dirname); 
  return true; 
}

 

This is your function, unmodified, i've just modified other parts so it would be called properly, i added error handling to make sure that the directory name gets an error if not found.  So we can go from here, try this and tell me what happens.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.