Here's the deal. I've got a "tree" or a "subtree" that I want to navigate and delete every element in. Each "node" may contain links to other nodes below it (no problem) OR may contain links OUTSIDE that particular "tree"/"subtree". How can I build a function that only deletes "within" the specified tree?
4
-
3you might want to give an example structure and desired outputGordon– Gordon2010-06-24 10:59:47 +00:00Commented Jun 24, 2010 at 10:59
-
Give an example of your "tree" and how the deletion should operate.salathe– salathe2010-06-24 12:39:42 +00:00Commented Jun 24, 2010 at 12:39
-
1I doubt it's homework when it's late June.Michael Stone– Michael Stone2010-06-24 13:11:15 +00:00Commented Jun 24, 2010 at 13:11
-
if a node points to another node, then the 'another' node is part of the tree. OR - what about nodes in other trees that point to the nodes you'd be deleting?? maybe you could clarify?Scott Evernden– Scott Evernden2010-06-24 21:02:55 +00:00Commented Jun 24, 2010 at 21:02
Add a comment
|
3 Answers
This is the same recursive delete that you're used to. You just have to keep your links separated - one list for in-tree links, one for out-of-tree links. Alternately, you can have a flag that keeps track of the in-tree/out-of-tree state for each link - but you're going to have to distinguish when you make the link.
Comments
1 Comment
slashingweapon
The link may be useful, but some brief explanation would make the answer more relevant.
You need to use realpath():
function DeleteTree($path)
{
if (is_dir($path) === true)
{
$path = realpath($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
$file = realpath($path . '/' . $file);
// file is within tree
if (substr($file, 0, strlen($path)) == $path)
{
DeleteTree($file);
}
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false;
}
The above should do what you're looking for.
Oh... I just realized this may not be related to the filesystem... The fault is all yours! :P