title: FTP Entire Directory using PHP with recursive function
body: <p>I am working on a code that will upload a folder of update files to several websites.  I have been trying to get the following function to upload all files and folders to the FTP account. In this case the local folder containing the files is named "files" and all documents are being uploaded to the parent directory of the ftp account.</p>
  
  <p><strong>Function</strong></p>
  
  <pre><code>function upload_dir($dirname,$conn_id) {
      $dir_array=array();
     if (is_dir($dirname))
        $dir_handle = opendir($dirname);
     if (!$dir_handle)
        return false;
     while($file = readdir($dir_handle)) {
        if ($file != "." &amp;&amp; $file != "..") {
           if (!is_dir($dirname."/".$file)){
               $dirname2= str_replace('files','',$dirname);
               if($dirname2[0]=='/'){
                  $dirname2 = substr($dirname2, 1);
               }
               ftp_put($conn_id, $dirname2."/".$file, $dirname."/".$file, FTP_BINARY);
           }
           else{
               $dirname2= str_replace('files','',$dirname);
               if($dirname2[0]=='/'){
                    $dirname2 = substr($dirname2, 1);
               }
  
               if(in_array($dirname2.'/'.$file, $dir_array)){
                   upload_dir($dirname.'/'.$file,$conn_id);
               }
               else{
                   array_push($dir_array, $dirname2.'/'.$file);
                   // check if directory exists before trying to create it
                      $filename = $dirname2.'/'.$file;
                      if (file_exists($filename)) {
                      } else {
                        ftp_mkdir($conn_id,$dirname2.'/'.$file);
                      }
                   upload_dir($dirname.'/'.$file,$conn_id);
               }
           }
        }
     }
     closedir($dir_handle);
     return true;
  }
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>// Ftp All data to new server
  $ftp_server = "ftp.mywebsite.com";
  // set up basic connection
  $conn_id = ftp_connect($ftp_server); 
  // login with username and password
  $login_result = ftp_login($conn_id, 'username', 'password'); 
  // check connection
  if ((!$conn_id) || (!$login_result)) { 
  $reply  = "FTP connection has failed!";
  $reply .= "Attempted to connect to $ftp_server for user zerozill@".$option1; 
  exit; 
  } else {
  $reply = "Connected to $ftp_server, for user annon";
  upload_dir('files',$conn_id);
  }
  // close the FTP stream 
  ftp_close($conn_id);
  </code></pre>
  
  <p>The issue I am having is that the function works for the most part but for some reason it refuses to upload folders at times.  For example I have a folder named scripts. Every time I run this function it will not upload the scripts folder.  As well it still tries to upload all the files to the folder creating errors that the scripts folder does not exist. I dont know if I am overlooking something or what but any help with this matter is appreciated.</p>