I am working on a website that has some files/folders that are "stored" on the webserver and viewed by the site user. I need to use some PHP FTP commands to manipulate the folders from an internal control panel.

The FTP is fully accessible and useable via 3rd party FTP software and/or Windows Network Neighborhood as well. We can add, edit, rename, delete files and folders.

I can get the FTP commands to work, all except for a FTP_RENAME. This one fails constantly no matter what parameters I pass to it. I've even tried using getcwd() to see if that mattered, but it didn't. I've tried full paths, literal paths, etc... in FTP_RENAME. None work.

The FTP_RENAME commands I use are:

$dir = "/web/proofs/";
$old_dirfolderfile=$dir."oldfolder";
$new_dirfolderfile=$dir."newfolder";
$conn_id = ftp_connect($FTP_SERVER);
ftp_login($conn_id, $FTP_UID, $FTP_PWD))
$result=ftp_rename($conn_id, $old_dirfolderfile,$new_dirfolderfile);
ftp_close($conn_id);

for reference, the code that creates the folder or deletes the folder is:

$dir = "/web/proofs/";
$old_dirfolderfile=$dir."oldfolder";
$conn_id = ftp_connect($FTP_SERVER);
ftp_login($conn_id, $FTP_UID, $FTP_PWD);

$result=ftp_mkdir($conn_id, $old_dirfolderfile);
-or-
$result=ftp_delall($conn_id, $old_dirfolderfile);

ftp_close($conn_id);

These commands work, just the FTP_RENAME fails.

I tried turning on additional error reporting with:

error_reporting(E_ALL);
ini_set('display_errors',1);

I checked the PHPINFO() and those are now enabled... but... that doesn't show anything at all so I am definately frustrated.

Not sure where to go with this now.

Any suggestions?

    Hi there pbrama,

    I would start by writing in some form of checks into the script. Right now, nobody could tell you at which part of the script it's stopping at because there's no place for it to let you know.

    This should at least let us figure out if it's failing on the renaming portion of the script:

    <?php
    
    $dir = "/web/proofs/";
    $old_dirfolderfile=$dir."oldfolder";
    $new_dirfolderfile=$dir."newfolder";
    $conn_id = ftp_connect($FTP_SERVER);
    if (!$conn_id) {
    	echo 'Connection to the FTP server failed.';
    	exit;
    }
    
    $login_result = ftp_login($conn_id, $FTP_UID, $FTP_PWD))
    if (!$login_result) {
    	echo 'We were unable to log into the FTP server.';
    	exit;
    }
    if(!file_exists($dir)){
    	echo 'Parent directory doesn&apos;t exist.';
    	exit;
    }
    if(!file_exists($old_dirfolderfile)){
    	echo 'Thefile or directory to be renamed doesn&apos;t exist: '.$old_dirfolderfile;
    	exit;
    }
    if(file_exists($new_dirfolderfile)){
    	echo 'There is already a file or folder at the following location: '.$new_dirfolderfile;
    	exit;
    }
    $result=ftp_rename($conn_id, $old_dirfolderfile,$new_dirfolderfile);
    if(!result)){
    	echo 'We were unable to rename '.$old_dirfolderfile.' to '.$new_dirfolderfile.'.';
    	exit;
    }
    
    ftp_close($conn_id);  
    
    ?>

      I copied your code to my test file... (had to make a couple typo fixes... not a problem) and got it to work. Problem is, now it goes all the way through and the $result is TRUE because I am getting...

      Rename Complete: ./proofs/test_folder/file.jpg to ./proofs/test_folder/file2.jpg

      but looking at the folder, nothing changes. I created some test files / folders manually to make sure all the "error" checking works, which it does. I tried it with a file, and a folder... no avail.

      Now I don't know WHY it is reporting TRUE when it doesn't do the actual rename.

      Here is the revised code:

      
      $dir = "./proofs/test_folder/";
      $old_dirfolderfile=$dir."file.jpg";
      $new_dirfolderfile=$dir."file2.jpg";
      
      $conn_id = ftp_connect($FTP_SERVER);
      if (!$conn_id) {
          echo 'Connection to the FTP server failed.';
          exit;
      }
      
      $login_result = ftp_login($conn_id, $FTP_UID, $FTP_PWD);
      if (!$login_result) {
          echo 'We were unable to log into the FTP server.';
          exit;
      }
      if (!file_exists($dir)){
          echo 'Parent directory doesn&apos;t exist.';
          exit;
      }
      if (!file_exists($old_dirfolderfile)){
          echo 'Thefile or directory to be renamed doesn&apos;t exist: '.$old_dirfolderfile;
          exit;
      }
      if (file_exists($new_dirfolderfile)){
          echo 'There is already a file or folder at the following location: '.$new_dirfolderfile;
          exit;
      }
      $result=ftp_rename($conn_id, $old_dirfolderfile,$new_dirfolderfile);
      if (!result){
          echo 'We were unable to rename '.$old_dirfolderfile.' to '.$new_dirfolderfile.'.';
          exit;
      } else {
      	echo 'Rename Complete: '.$old_dirfolderfile.'   to   '.$new_dirfolderfile.'.';
      }
      
      ftp_close($conn_id);  
      
      

      Any other ideas?

        I feel I should point out that file_exists is checking the local files, while ftp_rename is for working with remote files. You'll need to use [man]ftp_nlist[/man] and [man]in_array[/man] to check if the file exists at the ftp endpoint.

          I probably should have checked to make sure he was using FTP on the local server. Oops for that 🙂

            Are the folders able to be renamed with the same authentication through an FTP app? Are the permissions and ownership OK on them?

              This brings a couple questions....

              but first, to answer schwim... yes, I can use a 3rd party app or even network neighborhood attaching to the remote server to manipulate the files/folders using the same credentials. Also, as a reminder, the ftp_mkdir and ftp_delall commands also work fine using the original code as I listed it. The only difference to get the $result to not be false in Shwims code (modified) was to change the original $dir dropping the '/web/' and changing it to './'

              So the question(s) that come up based on both responses...
              - For the file_exists checking, that was referencing the server files as well. If I add the $new_dirfolderfile manually using Network Neightborhood or another FTp, then the file_exists errors out when it checks to make sure the new name doesn't exist. So my question... if file_exists only looks at local server, why does it work with my remote server?

              • As was mentioned, the ftp_rename would need to look at the remote server path differently.... what should that be then? I tried adding a GETCWD() to it but that didn't work either.

              Thanks

              Pete

                try checking the contents of the working ftp directory with nlist, as suggested by derokorian. We can at least check to make sure that the folders are seen in your current working directory.

                http://php.net/ftp_nlist

                // set up basic connection
                $conn_id = ftp_connect($ftp_server);
                
                // login with username and password
                $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
                
                // get contents of the current directory
                $contents = ftp_nlist($conn_id, ".");
                
                // output $contents
                var_dump($contents);

                  Ok, ran this code...

                  Got back the following....

                  .
                  ..
                  cgi-bin
                  log
                  ssl
                  tmp
                  web

                  just to recap, the "web" folder is where "/proofs/test_folder/" is located. So if I use "/web/proofs/test_folder/" as the $dir and add "test.jpg" for the file name to $old and add "test2.jpg" for the $new... then I get the failure for the ftp_rename. basically... ftp_rename($conn_id,"/web/proofs/test_folder/test.jpg", "/web/proofs/test_folder/test2.jpg")... this returns FALSE. I tried using ftp_chdir("/web/proofs/test_folder") and then calling the ftp_rename with just the file names, that didn't work either.

                  It's amazing that something simple, even after I use the other FTP commands with no problem, doesn't want to cooperate.

                    pbrama;11046119 wrote:

                    if I use "/web/proofs/test_folder/" as the $dir and add "test.jpg" for the file name to $old and add "test2.jpg" for the $new... then I get the failure for the ftp_rename. basically... ftp_rename($conn_id,"/web/proofs/test_folder/test.jpg", "/web/proofs/test_folder/test2.jpg")... this returns FALSE. I tried using ftp_chdir("/web/proofs/test_folder") and then calling the ftp_rename with just the file names, that didn't work either.

                    It's amazing that something simple, even after I use the other FTP commands with no problem, doesn't want to cooperate.

                    Does it still fail if you use a relative path (./web) instead of absolute (/web)?

                      I got it to finally work by changing the $dir to "/web/proofs/test_folder/"... although I KNOW I tried that before... must have overlooked something. Of course, all the FILE_EXISTS fail but I believe that is the LOCAL vs REMOTE reason. I assume I have to use the nlist and some additional code to verify all those.

                      But here is the oddity.....

                      If there is an issue, such as the OLD file/folder doesn't exist, or the NEW file/folder exists already, I still get a $result of TRUE even though the FTP_RENAME does not change anything. Is this normal behavior... if so, what results in a FALSE then? It's kinda of funny, went from constant FALSEs to constant TRUEs.

                        I believe this will check for the existence of the remote file or folder:

                        /* Get the remote contents array */
                        $remote_contents = ftp_nlist($conn_id, $directoryToCheckIn);
                        
                        /* Find out if your folder or file is listed */
                        if (in_array($filenameToCheck, $remote_contents)) {
                        	/* Your folder or file was found */
                        }else{
                        	/* No file or folder found */
                        }
                          Write a Reply...