Can't get SFTP to work in PHP

2020-07-02 08:11发布

I am writing a simple SFTP client in PHP because we have the need to programatically retrieve files via n remote servers. I am using the PECL SSH2 extension.

I have run up against a road block, though. The documentation on php.net suggests that you can do this:

$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');

However, I have an ls method that attempts to something similar

public function ls($dir)
{
    $rd = "ssh2.sftp://{$this->sftp}/$dir";
    $handle = opendir($rd);
    if (!is_resource($handle)) {
        throw new SFTPException("Could not open directory.");
    }

    while (false !== ($file = readdir($handle))) {
        if (substr($file, 0, 1) != '.'){
            print $file . "\n";
        }
    }
    closedir($handle);
}

I get the following error:

PHP Warning:  opendir(): Unable to open ssh2.sftp://Resource id #5/outgoing on remote host

This makes perfect sense because that's what happens when you cast a resource to string. Is the documentation wrong? I tried replacing the resource with host, username, and host and that didn't work either. I know the path is correct because I can run SFTP from the command line and it works fine.

Has anyone else tried to use the SSH2 extenstion with SFTP? Am I missing something obvious here?

UPDATE:

I setup sftp on another machine in-house and it works just fine. So, there must be something about the server I am trying to connect to that isn't working.

10条回答
别忘想泡老子
2楼-- · 2020-07-02 09:00

I just had the same issue, but I could figure out the problem.

On my case, when connecting to the server, I was going to the root of the account, and due to server configs I wasn't able to write there.

I have connected to the account using a fireFTP, and so I could see where the root of the account was...it was the root of the server.

I had to include the whole path until the folder where I am allowed to write, and so I could solve the issue.

So, my advice is to get the path using a graphic interface (I have used fireFTP), and add the whole path to your code.


$pathFromAccountRootFolderToMyDestinationFolder = '/Account/Root/Folder/To/My/Folder';
$stream = fopen("ssh2.sftp://".$sftp."/".$pathFromAccountRootFolderToMyDestinationFolder."/myFile.ext", 'r');

Hope this will help you and other people with the same issue!

Cheers!

查看更多
贼婆χ
3楼-- · 2020-07-02 09:03

My issue was, that I was connecting in function and returning string URL with resource inside. Unfortunatelly resource is than created in function context and garbage collector is disconnecting resource on function end. Solution: return resource by reference and unset it manually in more complex context.

查看更多
劳资没心,怎么记你
4楼-- · 2020-07-02 09:13

I'm having a similar issue. I assume you are doing something similar to this:

    $dir = "ssh2.sftp://{$sftp}{$remote_dir}"; 
    $handle = opendir($dir);

When $remote_dir is the full path from root then open_dir works. If $remote_dir is just '/' or '', then I get the 'unable to open' error as you did.

In my case, it seems ssh connects at the root folder instead of the 'home' directory as ftp does. You mentioned that it worked on a different server, so I wonder if it is just a config issue.

查看更多
我只想做你的唯一
5楼-- · 2020-07-02 09:13

the most easiest way to get SFTP working within PHP (even on windows) without installing any extension etc is PHPSECLIB: http://phpseclib.sourceforge.net/ . The SSH stuff is completely implemented in a PHP class.

You use is like this:

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

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

echo $sftp->pwd();
?>
查看更多
登录 后发表回答