Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick question about rmdir 1

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Sheesh!

I have written a script that automatically opens two folders upon a new user registering with the site. The first folder is the usernames name, and the second folder (inside the first) is called "images". Both are set to chmod 0777 for now (still in testing).

When I delete a member from an admin script I have written I want to be able to delete both of these folders (as well as the usual clearing stuff out of the database etc).

I am using a function that will do this but I think I have a path problem. Bear with me on this...

First thing to note is that the function definately works if it is in the same directory as the folders that I want to delete. However, I want to move it out of that section and put it into an admin folder on my site. These are the paths...

html_pages/folderA1/folderA2
html_pages/folderB1/folderB2

*N.B These are mythical folder names but the idea is the same*

The script to delete the folders is in folderA2 and the folder I want to delete is folderB2.

When my script was in html_pages I used the path './folderB1/folderB2' and this deleted it.

Now.. the script is in folderA2.. what should the path be??

I have tried '../../folderB1/folderB2' but to no avail. I have also tried the full unix path to the folder, but to no avail. I've come up against a brick wall....

Has anyone any ideas ? Pleeeeaase before I tear my last two hairs out !

Thanks for any help you can give me (I hope I've made the problem clear enough)

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
the path is correct (../../folderB1/foldeB2)

rmdir('../../folderB1/folderB2') will only work if the directory is empty.

if its not, you'll need to either list the contents and unlink each file first or use exec() to execute rm -rf <directoryname> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR

Doh! That explains it!

Thanks for your help mate.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
I don't suppose you know of any snippet of code that will (when a path is passed to it) unlink all files within a directory and then delete the directory ??

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
He posted one in his post. exec(&quot;rm -fr &quot; . $path); should do what you want. //Daniel
 
This should do the trick then, freshly written for the job, aren't I being nice today :)

<?php
// Get all the files that fit the search and are to be added to the menu
// set dir to be emptied and deleted here
$my_dir='../test';

function getdirArray($dir,$sort='asort') {

global $dir_file_count;

if ( is_dir($dir) ) {

$fd = @opendir($dir);

while ( ($part = @readdir($fd)) == TRUE ) {

clearstatcache();

if ($part != &quot;.&quot; && $part != &quot;..&quot; && (ereg(&quot;\\.*\$&quot;,$part))) {

$dir_array[] = $part;
}
}

if($fd == TRUE) {
closedir($fd);
}

if (is_array($dir_array)) {

$sort($dir_array);

$dir_file_count = count($dir_array);

Return $dir_array;

} else {

Return FALSE;
}

} else {

Return FALSE;

}

}

$file_array = getdirArray($my_dir,'asort');

foreach ($file_array as $file_name) {
unlink(&quot;$my_dir/$file_name&quot;);

echo &quot;unlink $my_dir/$file_name<br>&quot;;
}
rmdir($my_dir);
?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Daniel, I am most definately against anyone having exec() on their websites, in the wrong hands hours of work can be lost in minutes. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR

A big thanks for that mate. When you say you are against people having exec() because it can cause damage in the wrong hands, well.. I've got wrong hands !!

I'm new to PHP (previously done some Perl and much of it is the same), but needed to make sure because, as you said, don't want to accidentally specify root and delete my entire site !

Thanks again for your time. Its people like you that make TekTips The place for help and advice.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
Glad to help, hope it works in the right way for you (tested here it worked really well. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR,

Its all up and running brilliantly using your script. I've hardcoded the first bit of the path into the delete script and pass the rest in through a function, so there can be no mishaps (famous last words!)

I've tested it and it works like a dream. A big thanks once again mate, its appreciated.

Cheers

JT I don't make mistakes, I'm merely beta-testing life.
 
well if its that useful, slap a star on it, I can tell you I really dont get enough of them round here :)

PS glad it helped you. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top