fanfavorite Posted July 17, 2007 Share Posted July 17, 2007 I am trying to delete a folder and all of its contents (folders, files) through ftp. I am trying to modify a script for deleting it locally. I don't get any errors with this, but nothing happens. Can someone point me in the right direction? Thanks. -JC function delDir($dirName,$conn_id) { if(file_exists($dirName)) { $dir = ftp_nlist($conn_id, $dirName); while($file = $dir->read()) { if($file != '.' && $file != '..') { if(is_dir($dirName.'/'.$file)) { delDir($dirName.'/'.$file,$conn_id); } else { ftp_delete($conn_id,$dirName.'/'.$file); } } } $dir->close(); @ftp_rmdir($conn_id,$dirName); } else { return false; } return true; } delDir($startpos,$conn_id); Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 17, 2007 Share Posted July 17, 2007 i was under the impression that ftp_nlist() returns an array, not an object - which means, there's no read() or close() method in it. perhaps change the while() to a foreach(), and kill the $dir->close() line? i could be missing something terribly obvious here. Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted July 17, 2007 Author Share Posted July 17, 2007 Thanks akitchin. I switched it to a foreach, but still doesn't do anything or give any errors. Any other ideas? function delDir($dirName,$conn_id) { if(file_exists($dirName)) { $dir = ftp_nlist($conn_id, $dirName); foreach($dir as $file) { if($file != '.' && $file != '..') { if(is_dir($dirName.'/'.$file)) { delDir($dirName.'/'.$file,$conn_id); } else { ftp_delete($conn_id,$dirName.'/'.$file); } } } ftp_rmdir($conn_id,$dirName); } else { return false; } return true; } delDir($startpos,$conn_id); Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 17, 2007 Share Posted July 17, 2007 well you'll likely want to return the value from delDir() to the upper level on each recursion - that way it will pass up the results from the lowest level. you may also want to do two things: change the error reporting to ensure that it's turned on, and perhaps make a more useful error system in the function itself so that you'd actually see where things are stopping: ini_set('display_errors', 1); error_reporting(E_ALL); function delDir($dirName,$conn_id) { if(file_exists($dirName)) { $dir = ftp_nlist($conn_id, $dirName); foreach($dir as $file) { if($file != '.' && $file != '..') { if(is_dir($dirName.'/'.$file)) { return delDir($dirName.'/'.$file,$conn_id); } else { $result = ftp_delete($conn_id,$dirName.'/'.$file); if ($result == FALSE) exit('deleting of file failed'); } } } $result2 = ftp_rmdir($conn_id,$dirName); if ($result2 == FALSE) exit('deleting of directory failed.'); } else { exit('directory does not exist'); } return true; } delDir($startpos,$conn_id); Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted July 17, 2007 Author Share Posted July 17, 2007 Ok I started figuring things out a bit. The file_exists was not working because that was checking locally, when I need it for ftp. Also, the is_dir does not work for ftp. Here is my code: function delDir($conn_id) { $dir = ftp_nlist($conn_id, '.'); foreach($dir as $file) { if($file != '.' && $file != '..') { if(@ftp_chdir($conn_id,$file)) { delDir($conn_id); } else { ftp_delete($conn_id,$file); } } } ftp_rmdir($conn_id,'.'); } ftp_chdir($conn_id,$startpos); delDir($conn_id); Now that deletes everything except the folders. What happens is it keeps going into one folder after the other deleting all files and then deletes the last directory. How would I go about going back and deleting all folders once they have all files deleted? Quote Link to comment 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.