我愿把名录的档案列入另一个服务器。
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.
我愿把名录的档案列入另一个服务器。
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.
您可使用 。
<?php
$connection = ssh2_connect( shell.example.com , 22);
ssh2_auth_password($connection, username , password );
$sftp = ssh2_sftp($connection);
$sftp_fd = intval($sftp);
$handle = opendir("ssh2.sftp://$sftp_fd/path/to/directory");
echo "Directory handle: $handle
";
echo "Entries:
";
while (false != ($entry = readdir($handle))){
echo "$entry
";
}
如果有人在奋力争取工作,你正在使用
$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 );
?>
已故当事人,但在包装单上发现这一点,。
编辑:安装后
composer require phpseclib/phpseclib:~3.0
此处为样本:
<?php
use phpseclib3NetSFTP;
require_once(dirname(__FILE__) . /../vendor/autoload.php );
$remote_user = getenv( REMOTE_USERNAME );
$remote_pass = getenv( REMOTE_PASSWORD );
$sftp = new SFTP( remote_server.example.com );
$sftp->login($user, $pass);
$path = /somepath_to_files ;
$rawlist = $sftp->rawlist($path, true);
if (!empty($rawlist)) {
foreach ($rawlist as $file) {
if (!empty($file)) {
$dt_utc = new DateTimeImmutable(date( Y-m-d h:i:s , $file->mtime));
$dt_nyc = $dt_utc->setTimezone(new DateTimeZone( America/New_York ));
echo "file: " . $file->filename . " with create time of " .
$dt_nyc->format( Y-m-d H:i:s T ) . "
";
}
}
} else {
// there s an empty directory
}
echo "end of list
";
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 ...
<?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 = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
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 ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
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? 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 ...