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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.