[SOLVED] Deleting directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

[SOLVED] Deleting directories

Post by s.dot »

I have a page where users can delete themselves from my website. I can delete database entries without a problem. However since my website is a picture hog, leaving their pictures on my server is one hell of a waste of space.

I've played around with the unlink() function, but that would involve me doing lots (i mean lots) of queries selecting pictures.

Does anyone know where I can find a script that will delete a given directory's contents, and then delete the folder? This would help me out a bunch. Also, it wouldn't hurt if it chmod 0777 before it deleted, I'm not quite sure of the permissions on these folders, it seems like whenever I set them to 777 they revert to their original permissions after a while.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we've had several threads recently regarding recursive directory handling.. might want to look at those..

as for your permissions reverting, it's likely a server script that walks the folder tree making sure the permissions are set "correctly"
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

read the user notes int he manual, there is a perfect script there ready made for you

http://uk2.php.net/manual/en/function.rmdir.php
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

....

Post by s.dot »

when I delete directories I get this error:

Code: Select all

Warning: opendir(/uploads/scrotaye775): failed to open dir: No such file or directory in /home/scottttt/public_html/deleteme.php on line 54

Warning: readdir(): supplied argument is not a valid Directory resource in /home/scottttt/public_html/deleteme.php on line 55

Warning: closedir(): supplied argument is not a valid Directory resource in /home/scottttt/public_html/deleteme.php on line 59

Warning: rmdir(/uploads/scrotaye775): No such file or directory in /home/scottttt/public_html/deleteme.php on line 60

Warning: opendir(/thumbs/scrotaye775): failed to open dir: No such file or directory in /home/scottttt/public_html/deleteme.php on line 66

Warning: readdir(): supplied argument is not a valid Directory resource in /home/scottttt/public_html/deleteme.php on line 67

Warning: closedir(): supplied argument is not a valid Directory resource in /home/scottttt/public_html/deleteme.php on line 71

Warning: rmdir(/thumbs/scrotaye775): No such file or directory in /home/scottttt/public_html/deleteme.php on line 72
This is the code that I have:

Code: Select all

// Delete Uploads Folder //
function deldir($dir) {
$dh=opendir("/uploads/".$_COOKIEї'username']."");
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } }
closedir($dh);
if(rmdir($dir)) { return true; } else { return false; } }

deldir("/uploads/".$_COOKIEї'username']."");

// Delete thumnails folder //
function deldir2($dir) {
$dh=opendir("/thumbs/".$_COOKIEї'username']."");
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } }
closedir($dh);
if(rmdir($dir)) { return true; } else { return false; } }

deldir2("/thumbs/".$_COOKIEї'username']."");
These *ARE* valid directories trying to be deleted.

Any help?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

they aren't valid directories.. you are trying to delete root level directories that don't exist, and you don't have access to. You want to delete a directory under your account.. which appears to be under /home/scottttt/public_html/...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

...

Post by s.dot »

WOW, thank you! I had no idea it was this simple, I had put this section of my site off for months because I had no idea of how to figure it out. THIS ONE IS SOLVED! :D :D :D :D :D
Post Reply