homepage Welcome to WebmasterWorld Guest from 23.20.63.27
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
rmdir help
chrissim




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

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);
}



 

penders




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

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.

chrissim




msg:4416793
 1:41 pm on Feb 12, 2012 (gmt 0)

hi,

Files unlinks were successful deleted but not able to remove the directory

penders




msg:4416795
 2:18 pm on Feb 12, 2012 (gmt 0)

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?

chrissim




msg:4416796
 2:28 pm on Feb 12, 2012 (gmt 0)

hi,

no error report generated after deleted all my files

penders




msg:4416799
 2:48 pm on Feb 12, 2012 (gmt 0)

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?

chrissim




msg:4416800
 3:11 pm on Feb 12, 2012 (gmt 0)

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);
}

penders




msg:4416808
 4:31 pm on Feb 12, 2012 (gmt 0)

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.

chrissim




msg:4416873
 1:26 am on Feb 13, 2012 (gmt 0)

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);
}

chrissim




msg:4416978
 2:28 pm on Feb 13, 2012 (gmt 0)

Could some show me how it can be done?

penders




msg:4416993
 3:03 pm on Feb 13, 2012 (gmt 0)

Did you try closing the directory handle (and perhaps chrdir()) as I mentioned above?

chrissim




msg:4417008
 3:45 pm on Feb 13, 2012 (gmt 0)

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);
}

penders




msg:4417058
 5:37 pm on Feb 13, 2012 (gmt 0)

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.
rocknbil




msg:4417068
 5:50 pm on Feb 13, 2012 (gmt 0)

Per your previous thread [webmasterworld.com]you could add some sort of error trapping to see exactly why it won't delete.

chrissim




msg:4417259
 6:48 am on Feb 14, 2012 (gmt 0)

Thanks guys for helping out on my script :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved