【问题标题】:failed to download the latest files using PHP, ssh2 via SFTP无法通过 SFTP 使用 PHP、ssh2 下载最新文件
【发布时间】:2017-06-02 13:51:17
【问题描述】:
#!/usr/bin/php
<?php
  $username = "backup";
  $password = "xxxxxxx";
  $url      = '192.168.1.100';
  // Make our connection
  $connection = ssh2_connect($url);

  // Authenticate
  if (!ssh2_auth_password($connection, $username, $password))
     {echo('Unable to connect.');}

  // Create our SFTP resource
  if (!$sftp = ssh2_sftp($connection))
     {echo ('Unable to create SFTP connection.');}


  $localDir  = 'file:///home/hhh/Downloads/dbs';
  $remoteDir = '/home/backup/Dropbox/dbs';
  // download all the files
  $dir = ('ssh2.sftp://' . $sftp . $remoteDir);
  $numberOfFiles = 10;
  $pattern = '/\.(aes|AES)$/'; // check only file with these ext.          
  $newstamp = 2;            
  $newname = "";


  if ($handle = opendir($dir)) {               
       while (false !== ($fname = readdir($handle)))  {            
         // Eliminate current directory, parent directory            
         if (preg_match('/^\.{1,2}$/',$fname)) continue;            
         // Eliminate other pages not in pattern            
         if (! preg_match($pattern,$fname)) continue;            
         $timedat = filemtime("$dir/$fname");
         $fils[$fname] = $timedat;            
         if ($timedat > $newstamp) {
            $newstamp = $timedat;
            $newname = $fname;
          }
         }
        }
  closedir ($handle);

  arsort ($fils, SORT_NUMERIC);
  sfor($i = 0; $i < $numberOfFiles ; $i++)
  $fils2 = array_keys($fils);
  $i = 0;

  foreach($fils2 as $s){
    $i++;
    echo "$i " . $s . "<br>\n";
    if($i == $numberOfFiles )break;
  }
  // $newstamp is the time for the latest file
  // $newname is the name of the latest file
  // print last mod.file - format date as you like            

$rttp = ssh2_scp_recv($connection, "$remoteDir/$newname", "$localDir/$newname")

?>

我一直在尝试使用 sftp 从目录下载最新的 FILES。我只下载了一个文件而不是 10 个。我还可以调整它以下载所有文件,但这不是我想要的。

我想让它工作,以便我能够下载一定数量的文件。

【问题讨论】:

    标签: php sftp php-5.6 ssh2-sftp


    【解决方案1】:
    #!/usr/bin/php
    
    <?php
    $username = "user";
    $password = "password";
    $url      = "host ip";
    // Make our connection
    $connection = ssh2_connect($url);
    
    // Authenticate
    if (!ssh2_auth_password($connection, $username, $password))
         {echo('Unable to connect.');}
    
    // Create our SFTP resource
    if (!$sftp = ssh2_sftp($connection))
         {echo ('Unable to create SFTP connection.');}
    
    //$dir
    $localDir  = "/path/to/localdir/".date('Y-m-d');
    exec("mkdir -p '$localDir'");
    echo $localDir;
    
    
    $remoteDir = "/path/to/remotedir";
     // download all the files
    $files = scandir ('ssh2.sftp://' . $sftp . $remoteDir);
    if (!empty($files)) {
      foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            if (substr($file, 0, 11)== date('d-M-Y')) {
    
                //date('d-M-Y', strtotime('yesterday') #for retriving the previous day
                # code...
                 // echo $file;
                  ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
    
            }
    
        }
      }
    }
     ?>
    

    这会从远程目录下载最新文件,并按日期创建一个新的本地目录,用于下载新的远程文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多