westman Posted February 14, 2012 Share Posted February 14, 2012 i whould like to know how to remove a user folder from my site. <?php$target = "myFolder";if (is_dir($target)) { rmdir($target); // Remove it now}?> this code is graet but in some users folders they will be files and some will be emptyThe rmdir() filesystem function in PHP will remove or delete a directory, only if the folder is empty. so how do i delete a folder nomater what is in it? Link to comment Share on other sites More sharing options...
TheGallery Posted February 14, 2012 Share Posted February 14, 2012 Check if the folder is empty, if not delete all of it's files and then delete the folder. I am not providing any code because i've never used the functions that are required before. I believe those are the functions you will have to use though. http://www.php.net/manual/en/function.scandir.phphttp://php.net/manual/en/function.unlink.php Link to comment Share on other sites More sharing options...
westman Posted February 14, 2012 Author Share Posted February 14, 2012 i know but in an array how does it work?i do not know the file names in the folder Link to comment Share on other sites More sharing options...
thescientist Posted February 14, 2012 Share Posted February 14, 2012 you use a loop to iterate over an array Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2012 Share Posted February 14, 2012 Here's a function to delete a directory: function delete_directory($directory, $empty=false){ if(substr($directory,-1) == DIRECTORY_SEPARATOR) { $directory = substr($directory,0,-1); } if(!is_dir($directory) || !is_readable($directory)) { return false; } else { $handle = opendir($directory); while (false !== ($item = readdir($handle))) { if($item != '.' && $item != '..') { $path = $directory.DIRECTORY_SEPARATOR.$item; if(is_dir($path)) { delete_directory($path); } else { unlink($path); } } } closedir($handle); if($empty == false) { if(!rmdir($directory)) { return false; } } return true; }} If you pass true as the second parameter then it will delete all files and subdirectories but leave the parent directory there. Link to comment Share on other sites More sharing options...
westman Posted February 15, 2012 Author Share Posted February 15, 2012 Dear Moderator,Your code looks super fly, however i do not understand how i can use it in my script. my directory is..."../user/$user_id" could you please put comments in your code for the noobs like me to understand Kind regards.Westman. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 15, 2012 Share Posted February 15, 2012 Thank you Member, I strive to write the flyest code. I would encourage you to look up each of those functions in the manual if you don't know what they do, but in general that is a recursive function (a function that calls itself). It checks if the directory name ends with a slash and removes that if so, then it checks if $directory is a readable directory and quits if it's not, then it gets a list of everything in the directory and loops through it. If each item is a directory then the function calls itself to delete that directory, or if it's a file then it deletes the file. Link to comment Share on other sites More sharing options...
westman Posted February 15, 2012 Author Share Posted February 15, 2012 ok so i can use...$directory = "../user/$user_id";and continue with the super fly code? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 15, 2012 Share Posted February 15, 2012 You would include the code above to define the delete_directory function in your script, and then you can call the function like any other function and pass it the name of the directory you want to delete. There are some examples of creating and using functions here: http://www.w3schools.com/php/php_functions.asp Link to comment Share on other sites More sharing options...
westman Posted February 15, 2012 Author Share Posted February 15, 2012 ok so if i run this script... <?php$directory = "../user/$user_id";function delete_directory($directory, $empty=false){ if(substr($directory,-1) == DIRECTORY_SEPARATOR){ $directory = substr($directory,0,-1); } if(!is_dir($directory) || !is_readable($directory)){ return false; } else{ $handle = opendir($directory); while (false !== ($item = readdir($handle))){ if($item != '.' && $item != '..'){ $path = $directory.DIRECTORY_SEPARATOR.$item; if(is_dir($path)){ delete_directory($path); } else{ unlink($path); } } } closedir($handle); if($empty == false){ if(!rmdir($directory)){ return false; } } return true; }}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-- remove user --/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////$sql_del = mysql_query("DELETE * FROM users WHERE id='$user_id' LIMIT 1");?> have i defined the right directory to delete (have i use the right verible)?or in better words, have i deleted flie "../user/$user_id"? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 15, 2012 Share Posted February 15, 2012 You can use whatever variable name you want, or none at all, but you need to actually run the function. All you've done is define the function. delete_directory("../user/$user_id"); Link to comment Share on other sites More sharing options...
westman Posted February 16, 2012 Author Share Posted February 16, 2012 hmmm i see so i use function delete_directory(); to run the function aboveand it would look like... <?php/////////// name the folder to be deleted/////////$directory = "../user/$user_id";//////////////////////////////////////////////////function delete_directory($directory, $empty=false){ if(substr($directory,-1) == DIRECTORY_SEPARATOR){ $directory = substr($directory,0,-1); } if(!is_dir($directory) || !is_readable($directory)){ return false; } else{ $handle = opendir($directory); while (false !== ($item = readdir($handle))){ if($item != '.' && $item != '..'){ $path = $directory.DIRECTORY_SEPARATOR.$item; if(is_dir($path)){ delete_directory($path); } else{ unlink($path); } } } closedir($handle); if($empty == false){ if(!rmdir($directory)){ return false; } } return true; }}///////////// run the function /////////////////delete_directory();////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-- remove user --/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////$sql_del = mysql_query("DELETE * FROM users WHERE id='$user_id' LIMIT 1");?> will that work? Link to comment Share on other sites More sharing options...
thescientist Posted February 16, 2012 Share Posted February 16, 2012 well, if you don't want it to do anything, then yes. But then you wouldn't be paying attention to JSG's example very well. the delete directory function expects at least one parameter, the directory to delete, with an optional second parameter. read what people are saying, and read the tutorials if you aren't sure. You can use whatever variable name you want, or none at all, but you need to actually run the function. All you've done is define the function. delete_directory("../user/$user_id"); You would include the code above to define the delete_directory function in your script, and then you can call the function like any other function and pass it the name of the directory you want to delete. There are some examples of creating and using functions here: http://www.w3schools...p_functions.asp or just try it. Link to comment Share on other sites More sharing options...
westman Posted February 16, 2012 Author Share Posted February 16, 2012 ok i changeddelete_directory();todelete_directory($directory); and in the tests i did it is working can any one conform that this is correct? Link to comment Share on other sites More sharing options...
TheGallery Posted February 16, 2012 Share Posted February 16, 2012 This is correct yes. You should really take a loot at the links provided about functions. You'll have to use them sooner or later. Link to comment Share on other sites More sharing options...
westman Posted February 17, 2012 Author Share Posted February 17, 2012 thank you so much every one Link to comment Share on other sites More sharing options...
WebEn2siaX Posted March 4, 2012 Share Posted March 4, 2012 great! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.