oceans Posted June 12, 2007 Share Posted June 12, 2007 Dear Friends, A member gave me this code function delete_folder($folder) { if(!is_dir($folder)) { trigger_error("'{$folder}' is not a folder", E_USER_ERROR); return false; } $folder = str_replace('\\', '/', $folder); if($folder[strlen($var)-1] == '/') { $folder = substr($folder,0,strlen($folder)-1); } $dh = opendir($folder); while($item = readdir($dh)) { if($item == '.' || $item == '..') { continue; } if(is_dir("{$folder}/{$item}")) { delete_folder("{$folder}/{$item}"); } else { unlink("{$folder}/{$item}"); } } closedir($dh); return rmdir($folder); } but some times, this removes not only the folder but also the parent folder, I can't understand, can any one help me to see why. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/55206-removing-folder-content/ Share on other sites More sharing options...
smc Posted June 12, 2007 Share Posted June 12, 2007 Well if it only does it sometimes that would lead me to believe it is a problem with the argument you are butting in for delete_folder($folder); I looked at your code but don't see anything wrong at a glance, then again my scripts rarely require me to dable in directory modification Quote Link to comment https://forums.phpfreaks.com/topic/55206-removing-folder-content/#findComment-272908 Share on other sites More sharing options...
oceans Posted June 12, 2007 Author Share Posted June 12, 2007 Dear SMC, I will give you an example: C:\grandparent\parent\folder I want to remove "folder" and its entire content, but leave C:\grandparent\parent alone. The problem is, it works fine and not fine somtimes. Can you suggest me please. the idea is to remove "folder" and its entire content, I did tried deleting using simple php comand, but then i had another problem, that is if the folder is not empty this php comand gives error mentioning the folder is not empty. Quote Link to comment https://forums.phpfreaks.com/topic/55206-removing-folder-content/#findComment-272915 Share on other sites More sharing options...
oceans Posted June 14, 2007 Author Share Posted June 14, 2007 OK Friends, I found the staff, the function is working perfectly as intended. Idea is to delete the folder and its entire contant! When I call this function, I call it like this delete_folder("NewFolder/".$ID); I think, if by chance $ID is "" (i.e. empty), the NewFolder gets deleted. (Please correct me if I am wrong). Can we make an addition in the function to say if the folder name is "NewFolder" don't delete it, will this help? Quote Link to comment https://forums.phpfreaks.com/topic/55206-removing-folder-content/#findComment-274480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.