PeejAvery
December 1st, 2005, 01:44 AM
Does anyone know an alternative code so I can delete a folder with contents in it. I know that rmdir requires that the directory be emtpy?
|
Click to See Complete Forum and Search --> : rmdir with files inside. PeejAvery December 1st, 2005, 01:44 AM Does anyone know an alternative code so I can delete a folder with contents in it. I know that rmdir requires that the directory be emtpy? cherish December 1st, 2005, 02:25 AM Hi peejavery, you might want to take a look at the function ftp_rmdir(). Here are some links: http://www.php.net/manual/en/function.ftp-rmdir.php http://aidanlister.com/repos/?file=function.ftp_rmdirr.php <-- a code from the second post in the first link I posted here. Hope those helps. :) PeejAvery December 1st, 2005, 11:34 AM I thought about that but not all the webservers I work with have FTP support. I was thinking of writing my own search function that would look in all sub folders and remove each of those files. Is this the only other option? NoHero December 1st, 2005, 11:56 AM I thought about that but not all the webservers I work with have FTP support. I was thinking of writing my own search function that would look in all sub folders and remove each of those files. Is this the only other option? You can still use rm with the -r (or -R...) -f (or -F force deletion) options to tell it to act recursively! (Warning doing rm -r / as a root is not recommended ;)) And then you can do rmdir on the directory. PeejAvery December 1st, 2005, 02:49 PM You can still use rm with the -r (or -R...) -f (or -F force deletion) options to tell it to act recursively! How can I tell PHP to do rmdir with syntax like that. I have never seen nor heard of that before. You make it sound like DOS. PeejAvery December 1st, 2005, 02:56 PM For those who might benefit from this, I found this code at http://lixlpixel.org (http://lixlpixel.org/recursive_function/php/recursive_directory_delete/). Still, if anyone knows an easier route, please let me know. function rrd($directory, $empty=FALSE){ if(substr($directory,-1) == '/'){$directory = substr($directory,0,-1);} if(!file_exists($directory) || !is_dir($directory)){return FALSE;} elseif(!is_readable($directory)){return FALSE;} else{ $handle = opendir($directory); while (FALSE !== ($item = readdir($handle))){ if($item != '.' && $item != '..'){ $path = $directory.'/'.$item; if(is_dir($path)){rrd($path);} else{unlink($path);} } } closedir($handle); if($empty == FALSE){if(!rmdir($directory)){return FALSE;}} return TRUE; } } blueday54555 December 2nd, 2005, 06:22 AM Well a easier way would be <?php function del_dir($dir) { if($objs = glob($dir."/*")){ foreach($objs as $obj) { is_dir($obj)? rmdirr($obj) : unlink($obj); } } rmdir($dir); } del_dir("c:\\tmp"); ?> PeejAvery December 2nd, 2005, 07:37 AM But it looks as thought that will only empty that specific folder. The one I posted is recursive and goes levels deep. blueday54555 December 2nd, 2005, 08:42 AM ahhh sh.... you are right. (never post a code you never tried yourself:)) <? function rmdirr($dirname) { if (!file_exists($dirname)) { return false; } if (is_file($dirname) || is_link($dirname)) { return unlink($dirname); } $dir = dir($dirname); while (false !== $entry = $dir->read()) { if ($entry == '.' || $entry == '..') { continue; } rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); } $dir->close(); return rmdir($dirname); } rmdirr("c:\\tmp"); ?> PeejAvery December 2nd, 2005, 09:49 AM ahhh sh.... you are right. (never post a code you never tried yourself:)) I think we all have done that before. Thanks for the input but I think I will stick with the one I have for now. I'll be back to rate your post later. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |