Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get SFTP to work in PHP

Tags:

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.

like image 246
Chris Kloberdanz Avatar asked Sep 23 '09 15:09

Chris Kloberdanz


People also ask

How to connect SFTP in PHP?

php // Download remote file to local file public function downloadFile($remote_file, $local_file) { fwrite(STDOUT, "Downloading [${remote_file}] to [${local_file}] ... \n"); $sftp = $this->sftp; $realpath = ssh2_sftp_realpath($sftp, $remote_file); $stream = @fopen("ssh2. sftp://$sftp$realpath", 'r'); if (!

Can I SSH to SFTP server?

SFTP cannot exist without SSH — SFTP uses SSH as the binding agent to transfer files securely. In other words, SSH protocol is used in the file transfer mechanism SFTP. In fact, most SSH servers include SFTP capabilities. However, not all SFTP servers support SSH commands and actions.

What is SFTP folder?

SFTP (Safe File Transfer Protocol) is part of the SSH protocol designed to securely transfer files between remote systems. It allows users to view, manage, and change file and directory permissions on remote systems.


1 Answers

When connecting to a SFTP server and you need to connect to the root folder (for instance for reading the content of the folder) you would still get the error when using just "/" as the path.

The solution that I found was to use the path "/./", that's a valid path that references to the root folder. This is useful when the user you are logging with has access only to its own root folder and no full path is available.

So the request to the server when trying to read the contents of the root folder should be something like this:

$rd = "ssh2.sftp://{$this->sftp}/./";
like image 181
Luis Gustavo Padilla López Avatar answered Sep 17 '22 19:09

Luis Gustavo Padilla López