Jump to content

Recommended Posts

Dear Friends,

 

A member gave me this code

 

function delete_folder($folder)
{
if(!is_dir($folder))
{
	trigger_error("'{$folder}' is not a folder", E_USER_ERROR);
	return false;
}
$folder = str_replace('\\', '/', $folder);

if($folder[strlen($var)-1] == '/')
{
	$folder = substr($folder,0,strlen($folder)-1);
}
$dh = opendir($folder);
while($item = readdir($dh))
{
	if($item == '.' || $item == '..')
	{
		continue;
	}
	if(is_dir("{$folder}/{$item}"))
	{
		delete_folder("{$folder}/{$item}");
	}
	else 
	{
		unlink("{$folder}/{$item}");
	}
}
closedir($dh);
return rmdir($folder);
}

but some times, this removes not only the folder but also the parent folder, I can't understand, can any one help me to see why. thanks.

Link to comment
https://forums.phpfreaks.com/topic/55206-removing-folder-content/
Share on other sites

Well if it only does it sometimes that would lead me to believe it is a problem with the argument you are butting in for delete_folder($folder);

 

I looked at your code but don't see anything wrong at a glance, then again my scripts rarely require me to dable in directory modification

Dear SMC,

 

I will give you an example:

 

C:\grandparent\parent\folder

 

I want to remove "folder" and its entire content, but leave C:\grandparent\parent alone.

 

The problem is, it works fine and not fine somtimes.

 

Can you suggest me please.

 

the idea is to remove "folder" and its entire content, I did tried deleting using simple php comand, but then i had another problem, that is if the folder is not empty this php comand gives error mentioning the folder is not empty.

OK Friends,

 

I found the staff, the function is working perfectly as intended.

 

Idea is to delete the folder and its entire contant!

 

When I call this function, I call it like this

 

delete_folder("NewFolder/".$ID);

 

I think, if by chance $ID is "" (i.e. empty), the NewFolder gets deleted.

(Please correct me if I am wrong).

Can we make an addition in the function to say if the folder name is "NewFolder" don't delete it, will this help?

::)

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.