What file operations are available with ssh2:// in PHP

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:26:48

问题


I'm rewriting a function that processed and sorted files on the local server with one that can do so on a remote server reached through an ssh connection.

The existing system uses the pecl ssh2 library to grab specific files such like:

if ($stream = @fopen("ssh2.sftp://$sftp/$filename", "r")) {
//do stuff...
}

But I don't know the filename already. I hade this working locally using

$file_path =  ABSOLUTE_PATH . UPLOAD_URL . $importfolder . '/';
$file = '*.xml';
$files = glob($file_path.$file);

I can't get glob() working through the ssh server

$files = glob("ssh2.sftp://$sftp/*.xml");

(always comes back blank despite a number of xml files in the directory (the sftp connection is to a single directory).

how can I acheive this? Can I use functions like glob() here?


回答1:


you could simply use the ssh2 functions...

ie.

$session = ssh2_connect('some.server', 22);
ssh2_auth_password($session, 'username', 'password');
$stream = ssh2_exec($session, 'ls -la /some/path');
stream_set_blocking($stream, true);
echo "list of files: " . stream_get_contents($stream);



回答2:


You could do this:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

print_r($sftp->nlist()); // == $sftp->nlist('.')
print_r($sftp->rawlist()); // == $sftp->rawlist('.')
?>

(uses phpseclib, a pure PHP SFTP implementation)



来源:https://stackoverflow.com/questions/12901040/what-file-operations-are-available-with-ssh2-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!