title: PHP download from remote server via sftp
body: <p>This may have been asked before, I'm new to PHP and I'm trying to learn as much as I can, but this has really thrown me.</p>
  
  <p>Basically what I want to know is, how would I use PHP code to get it to download everything from a remote server to a local location. It's getting it to download everything not just one file that I'm stuck on. So please can someone show/explain to me how I would do this?</p>
  
  <p>What I've got so far:</p>
  
  <pre><code>&lt;?php
  $connection - ssh2_connect('example.com', 22);
  ssh2_auth_password($connection, 'username', 'password');
  
  $remote_dir="/remote_dir/";
  $local_dir="/local_dir/";
  
  $remote ="$remote_dir";
  $stream = ssh2_exec($connection, $remote);
  stream_set_blocking($stream,true);
  $command=fread($stream,4096);
  
  $array=explode(\n,$command);
  
  $total_files=sizeof($array);
  
  for($i=0;$i&lt;$total_files;$i+++){
      $file_name=trim($array[$i]);
      if($file_name!=''{
          $remote_file=$remote_dir.$file_name;
          $local_file=$local_dir.$file_name;
  
          if(ssh2_scp_recv($connection, $remote_file,$local_file)){
              echo "File ".$file_name." was copied to $local_dir&lt;br /&gt;"; 
          }
      }
  }
  fclose($stream);
  ?&gt;
  </code></pre>
  
  <p>I think my $remote ="$remote_dir"; is wrong, and to be honest I've got $filename when I want the whole directory, this is all I have so far.</p>