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
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); 
		}
	}
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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); 
		}
	}
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

thank you :D that works i had to change the part below because it deleted all folders and images instead of the selected folder.

 

if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{
$dirname = $path.$file;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.