Jump to content

Delete Folder FTP


fanfavorite

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/60436-delete-folder-ftp/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/60436-delete-folder-ftp/#findComment-300646
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/60436-delete-folder-ftp/#findComment-300658
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/60436-delete-folder-ftp/#findComment-300662
Share on other sites

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? 

Link to comment
https://forums.phpfreaks.com/topic/60436-delete-folder-ftp/#findComment-300703
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.