dpd20130 2017-06-02 13:51
浏览 150
已采纳

无法通过SFTP使用PHP,ssh2下载最新文件

#!/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>
";
    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")

?>

I have been trying to download the latest FILES from a directory using sftp. I have only managed to download ONE file instead to 10. I also was able to tweak it to download all the files but that is not i what I was after.

I would like to make it work so that I can be able to download a certain X number of files.

  • 写回答

1条回答 默认 最新

  • dou29106 2017-07-05 10:07
    关注
    #!/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");
    
            }
    
        }
      }
    }
     ?>
    

    This downloads the latest files from a remote directory and creates a new local directory by date where it downloads the new remote files

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题