English 中文(简体)
• 如何在使用sh2的服务器中填写名录
原标题:How to list files of a directory in an other server using ssh2
  • 时间:2012-01-12 19:11:40
  •  标签:
  • php
  • ssh

我愿把名录的档案列入另一个服务器。

I am connected to an other server using ssh2_connect function the connection is going well and I am able to fetch a desired file but I am not sure how the files can be listed.

最佳回答
问题回答

如果有人在奋力争取工作,你正在使用。 最近的一项更新是,要么设定了要求,要么引入“钟码”(<>intval(>)必须用于所有社会、社会、文化权利委员会的夹/档案检索功能:

$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");

这里采用的方法是,如果确定补偿性参数,可以重新扫描目录,并将多功能阵列退回,或者只扫描路面,如果该表没有,则退回一个包含该目录档案的单尺寸阵列。 可作修改,如果需要,也可列入无内容的目录。

把它作为一类,很容易在以后再利用。 我只包括需要回答问题的本班方法。

$host      =  example.com ;
$port      = 22;
$username  =  user1 ;
$password  =  password123 ;
$path      =  . ;
$recursive = true;

$conn = new SFTP($host, $port);
$conn->login($username, $password);
$files = $conn->ls($path, $recursive);
var_dump($files);

class SFTP
{
  private $connection;
  private $sftp;

  public function __construct($host, $port = 22)
  {
    $this->connection = @ssh2_connect($host, $port);
    if (! $this->connection)
      throw new Exception("Could not connect to $host on port $port.");
  }

  public function login($username, $password)
  {
    if (! @ssh2_auth_password($this->connection, $username, $password))
      throw new Exception("Could not authenticate with username $username");
    $this->sftp = @ssh2_sftp($this->connection);
    if (! $this->sftp)
      throw new Exception("Could not initialize SFTP subsystem.");
  }

  public function ls($remote_path, $recursive = false)
  {
    $tmp      = $this->sftp;
    $sftp     = intval($tmp);
    $dir      = "ssh2.sftp://$sftp/$remote_path";
    $contents = array();
    $handle   = opendir($dir);

    while (($file = readdir($handle)) !== false) {
      if (substr("$file", 0, 1) != "."){
        if (is_dir("$dir/$file")){
          if ($recursive) {
            $contents[$file] = array();
            $contents[$file] = $this->ls("$remote_path/$file", $recursive);
          }
        } else {
          $contents[] = $file;
        }
      }
    }

    closedir($handle);
    return $contents;
  }
}

rel=“nofollow” http://www.php.net/manual/en/Function.ssh2-exec.php

请注意<代码>ls的指挥,假定其为基于UNIX的系统(通常情况下是这种情况),否则,如<代码>dir<的Windows。

<?php
$connection = ssh2_connect( shell.example.com , 22);
ssh2_auth_password($connection,  username ,  password );

$stream = ssh2_exec($connection,  ls );
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签