Hello!

I'm having trouble copying a directory and it's contents from my pc to my server.

I can connect ok but the upload always fails. The permissions on the destination directory are 777 and I have also tried passive mode but that didn't work either.

Many thanks for your help! 🙂

$source_file = 'C:\Events\EVENT_' . $event_num;
$destination_file = '/home/cberry/public_html/test1/images/event_images/';

// login with username and password
// turn passive mode on
	$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass); 
	ftp_pasv ($conn_id, true);

// check connection
if ((!$conn_id) || (!$login_result)) { 
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
       exit; 
   } else {
       echo "Connected to $ftp_server, for user $ftp_user ";
   }

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
   }

// close the FTP stream 
ftp_close($conn_id); 

    With ftp_put() you can't upload a whole directory but only one file at a time.

    Look here : http://fr3.php.net/manual/en/function.ftp-put.php

    Extract from examples :

    Here is a fix for the function from lucas at rufy dot com below that will recursively put files from a source directory to a destination directory.  As written below, it won't put a file in a directory that already exists, because the the destination is altered.  So here is the corrected function that will allow it to work:
    
    function ftp_putAll($conn_id, $src_dir, $dst_dir) {
       $d = dir($src_dir);
       while($file = $d->read()) { // do this for each file in the directory
           if ($file != "." && $file != "..") { // to prevent an infinite loop
               if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
                   if (!@ftp_nlist($conn_id, $dst_dir."/".$file)) {
                       ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
                   }
                   ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
               } else {
                   $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
               }
           }
       }
       $d->close();
    }
    

      Thanks! I was wondering about that.

      I changed the code but I still can't get it to work 😕

      Do I need a trailing \ on $source_file to get the files inside \EVENT_xxx ?

      The /event_images directory does already exist, permissions are 777

      Here's my new code with the above change. Thanks again for your help!!!

      $source_file = 'C:\Events\EVENT_' . $event_num ;
      $destination_file = '/home/cberry/public_html/test1/images/event_images/';
      
      // upload the file
      //$fp = fopen($destination_file);
      //$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
      function ftp_putAll($conn_id, $source_file, $destination_file) { 
         $d = dir($source_file); 
         while($file = $d->read()) { // do this for each file in the directory 
             if ($file != "." && $file != "..") { // to prevent an infinite loop 
                 if (is_dir($source_file."/".$file)) { // do the following if it is a directory 
                     if (!@ftp_nlist($conn_id, $destination_file ."/".$file)) { 
                         ftp_mkdir($conn_id, $destination_file."/".$file); // create directories that do not yet exist 
                     } 
                     ftp_putAll($conn_id, $source_file."/".$file, $destination_file."/".$file); // recursive part 
                 } else { 
                     $upload = ftp_put($conn_id, $destination_file."/".$file, $source_file."/".$file, FTP_BINARY); // put the files 
                 } 
             } 
         } 
         $d->close(); 
      }

        Have you call the ftp_putAll() function somewhere ?

        You can try :

         $source_file = 'C:[color=red]/[/color]Events[color=red]/[/color]EVENT_' . $event_num ; 

          Also just to be sure : you are using a PHP script on your PC, inside a local web server
          which is on your own PC, to transfer files from your PC to another web server somewhere
          on Internet ?

          Or are you trying to execute this PHP script on the web server on Internet ?
          Trying to pull files from your local PC to the Internet server, doing that from the Internet server ?

          This last option will never work because the Internet web server has no idea of what "c:/...." mean.
          It has no access to your local files.

            Write a Reply...