PHP - ftp_put(), move_uploaded_file() problem

tripwater

Registered
Hello, I have a site that allows developers to upload current modules to the webserver. It has been requested that when this is done, I also ftp a backup copy to a remote server. Something is wrong here. The file upload code works. The back ftp code works,only if I comment out the move_uploaded_file() function. I can not get them both to work. After the move_uploaded_file() function moves the file to the webserver, the ftp_put() fails, but if I comment out the move_uploaded_file(), the ftp_put() works. Can anyone help me with this? I have posted this on other forums and working on this for a couple of weeks now.


1. I am uploading the module to the webserver.
2. Then in the next step want to ftp same module to backup server.

So Step 1


Code:
$path    = "../modules"; 

if (!is_dir($path)) 
   mkdir($path,0700);       
    
    
if (!move_uploaded_file($_FILES['srcfile']['tmp_name'], $path."/".strtolower(trim($_FILES['srcfile']['name']))))

Which works. THis uploads the module to the webserver to the set location


then Step 2


Code:
$ftp = ftp_connect('server goes here'); 

          
// login with username and password 
if (ftp_login($ftp, 'username', 'password')) 
   { 
   ftp_pasv ( $ftp, true ); 
             
             
             
   if (ftp_put($ftp, "public_html/temp/" . $_FILES['srcfile']['name'], $_FILES['srcfile']['tmp_name'], FTP_BINARY)) 
      { 
      $message = "File uploaded"; 
      } 
      else 
      { 
      die("Could not upload file"); 
      } 
   } 
else 
   { 
   die("Could not login to FTP account"); 
   } 
             
if (!empty($message)) 
   { 
   echo $message; 
   } 
          
// close the FTP stream 
ftp_close($ftp);


The only way this second FTP code works is if I comment out the first upload code,

Code:
$path    = "../modules"; 

if (!is_dir($path)) 
   mkdir($path,0700);       
    
    
//if (!move_uploaded_file($_FILES['srcfile']['tmp_name'], $path."/".strtolower(trim($_FILES['srcfile']['name']))))
Can anyone tell me if the problem is a ftp stream being left open and that is why I can not get the second portion of my code to work? Is there a way to get both of these to work together? I need not only to be able to upload to the webserver but ftp a backup copy to the remote server as well.

Thank you for any help.
 

yakasha

Registered
You used move_uploaded_file() to move the file to $path.

php manual.move_uploaded_file said:
If the file is valid, it will be moved to the filename given by destination
The file is no longer in temp.

try ftp_put($ftp, $path)
 

tripwater

Registered
After nearly 2 weeks, 4 different forums, some posts viewed over 1500 times and also posting to 2 different news groups researching a few php books you are the man!!!!!!

Thank you for pointing this simple mistake. Wow...
 
Top