Welcome to WebmasterWorld Guest from 54.162.240.235

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

rmdir help

     
12:44 pm on Feb 12, 2012 (gmt 0)

5+ Year Member



Could some one help me out what is wrong with my code below as i unable to remove the directory folder($dirfol). The $dirfol contained some files and the code below managed to deleted them out before removing the directory but somehow it won't.




if (isset($_POST['rmfolder'])) {
$dirfol = $_POST['deldir'];
$dir = opendir($dirfol);
while(FALSE !== ($file = readdir($dir))){
if($file == '.' or $file == '..') {continue;}
if(is_file($dirfol. "/" . $file)){
unlink($dirfol . "/" . $file);
} else{
$subDirectory = $dirfol . "/" . $file;
$subdir = opendir($subDirectory);
while(FALSE !== ($subdirFile = readdir($subdir))){
if($subdirFile == '.' or $subdirFile == '..') {continue;}
unlink($dirfol . "/" . $file . "/" . $subdirFile);
}
rmdir($subDirectory);
}
}
rmdir($dirfol);
}


1:25 pm on Feb 12, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Do you get any errors / warnings?

...managed to deleted them out before removing the directory but somehow it won't.


Are you saying the files were deleted or not? unlink() was successful?

My initial thought is a permissions problem...? Unless your script created the folder you may not have permissions to delete it.
1:41 pm on Feb 12, 2012 (gmt 0)

5+ Year Member



hi,

Files unlinks were successful deleted but not able to remove the directory
2:18 pm on Feb 12, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



No E_WARNING generated? The reason I ask is that rmdir() [uk.php.net] is supposed to generate an error of level E_WARNING on failure - and this would give a clue as to what the problem is. Make sure your error_reporting() level set sufficiently. rmdir() is presumably returning (bool)false?
2:28 pm on Feb 12, 2012 (gmt 0)

5+ Year Member



hi,

no error report generated after deleted all my files
2:48 pm on Feb 12, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I'm assuming you're running this on Linux?

Did PHP create the folder in the first place? Who is the owner of the folder? What are the permissions on the folder? What user does PHP run as?
3:11 pm on Feb 12, 2012 (gmt 0)

5+ Year Member



hi,

my php created the folder and im the owner.Permission on the folder is 755 but i did change the permission to 777 but still unable to remove using the script above.
But if the directory are empty i can removed them using this simple function below

if (isset($_POST['rmfolder'])) {
$dirfol= $_POST['deldir'];
rmdir($dirfol);
}
4:31 pm on Feb 12, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Ah OK, so permissions are OK it seems - without knowing the background this is probably the most common cause, hence my badgering. :)

You are not closing the directory handle before calling rmdir(), I would have thought you would need to call closedir($subdir) and closedir($dir) respectively before trying to remove these directories. Also, you could try chdir() to the parent folder before attempting to delete it.
1:26 am on Feb 13, 2012 (gmt 0)

5+ Year Member



hi

Ok the script below managed to clear out all files and subfolder inside
top level folder A but i need to remove completely folder A as well.
Any one has the clue how it can be done using the script below?

-Folder A
files
files
-subfolder
files


if (isset($_POST['rmfolder'])) {
$dirfol = $_POST['deldir'];
$dir = opendir($dirfol);
while(FALSE !== ($file = readdir($dir))){
if($file == '.' or $file == '..') {continue;}
if(is_file($dirfol. "/" . $file)){
unlink($dirfol . "/" . $file);
} else{
$subDirectory = $dirfol . "/" . $file;
$subdir = opendir($subDirectory);
while(FALSE !== ($subdirFile = readdir($subdir))){
if($subdirFile == '.' or $subdirFile == '..') {continue;}
unlink($dirfol . "/" . $file . "/" . $subdirFile);
}
rmdir($subDirectory);
}
}
rmdir($dirfol);
}
2:28 pm on Feb 13, 2012 (gmt 0)

5+ Year Member



Could some show me how it can be done?
3:03 pm on Feb 13, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Did you try closing the directory handle (and perhaps chrdir()) as I mentioned above?
3:45 pm on Feb 13, 2012 (gmt 0)

5+ Year Member



hi,

Can you guide me the full complete script where should i closedir and how to chdir so i can delete the parent folder. im a newbie to php so appreciate if you can amend my full script where i can delete the parent folder.

Thanks



if (isset($_POST['rmfolder'])) {
$dirfol = $_POST['deldir'];
$dir = opendir($dirfol);
while(FALSE !== ($file = readdir($dir))){
if($file == '.' or $file == '..') {continue;}
if(is_file($dirfol. "/" . $file)){
unlink($dirfol . "/" . $file);
} else{
$subDirectory = $dirfol . "/" . $file;
$subdir = opendir($subDirectory);
while(FALSE !== ($subdirFile = readdir($subdir))){
if($subdirFile == '.' or $subdirFile == '..') {continue;}
unlink($dirfol . "/" . $file . "/" . $subdirFile);
}
rmdir($subDirectory);
}
}
rmdir($dirfol);
}
5:37 pm on Feb 13, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



if (isset($_POST['rmfolder'])) { 
$dirfol = $_POST['deldir'];
$dir = opendir($dirfol);
while (FALSE !== ($file = readdir($dir))) {
if($file == '.' or $file == '..') {continue;}
if(is_file($dirfol. "/" . $file)){
unlink($dirfol . "/" . $file);
} else{
$subDirectory = $dirfol . "/" . $file;
$subdir = opendir($subDirectory);
while (FALSE !== ($subdirFile = readdir($subdir))) {
if($subdirFile == '.' or $subdirFile == '..') {continue;}
unlink($dirfol . "/" . $file . "/" . $subdirFile);
}
closedir($subdir); // <<< CLOSE DIR HANDLE BEFORE DELETE DIR
rmdir($subDirectory);
}
}
closedir($dir); // <<< CLOSE DIR HANDLE BEFORE DELETE DIR
rmdir($dirfol);
}


As mentioned above, you must close the directory handle of the directory you wish to delete before attempting to delete it. This should resolve your problem. This should also have given you a "permission denied" warning, but I'm guessing your error_reporting() level is suppressing E_WARNINGs.

WARNING: I should add that your code appears to have a BIG security hole... you appear to be passing in the directory to be deleted in the POST data, without any validation. This could be hacked by a malicious user and potentially delete everything on your server! This needs some validation.
5:50 pm on Feb 13, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Per your previous thread [webmasterworld.com]you could add some sort of error trapping to see exactly why it won't delete.
6:48 am on Feb 14, 2012 (gmt 0)

5+ Year Member



Thanks guys for helping out on my script :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month