php-ssh icon indicating copy to clipboard operation
php-ssh copied to clipboard

SFTP - Error when sending file

Open anthonyherve opened this issue 10 years ago • 12 comments

Hello,

I have a problem when I want to send a file through SFTP. This is the following.

[Symfony\Component\Debug\Exception\ContextErrorException] Warning: file_put_contents(): Unable to open ssh2.sftp://Resource id #568/${path_remote} on remote host

Here is my code :

$configuration = new Configuration('url'); $authentication = new Password('username', 'password'); $session = new Session($configuration, $authentication); $sftp = $session->getSftp(); $sftp->send("local_file", "remote_file");

Error is on the last line.

Can you help me please ?

Thanks.

Anthony

anthonyherve avatar Feb 12 '15 14:02 anthonyherve

Does the file get transferred? Is it the only warning you get?

h4cc avatar Feb 13 '15 08:02 h4cc

Hello !

Thank you for your answer. Unfortunately, my file does not get transferred.

Have you maybe another idea ? :-)

anthonyherve avatar Feb 13 '15 08:02 anthonyherve

It would help to know if SCP works at all beforehand. Could you try to transfer the file using scp like described here?

http://www.hypexr.org/linux_scp_help.php

h4cc avatar Feb 13 '15 08:02 h4cc

Yes I can transfer file with SCP command.

anthonyherve avatar Feb 13 '15 08:02 anthonyherve

@anthonyherve Whats your PHP Version?

I can confirm this bug, using this version of PHP:

$ php -v
PHP 5.6.5-1+deb.sury.org~trusty+1 (cli) (built: Jan 26 2015 11:42:37) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

h4cc avatar Feb 13 '15 09:02 h4cc

Here is my version :

PHP 5.5.21 (cli) (built: Jan 21 2015 14:44:22)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

anthonyherve avatar Feb 13 '15 10:02 anthonyherve

I reran the last travis builds, and the functional tests there worked: https://travis-ci.org/Herzult/php-ssh/builds/49846322

Event against our PHP Versions 5.5.21 and 5.6.5.

I honestly dont know whats wrong here :weary:

h4cc avatar Feb 13 '15 10:02 h4cc

Probably related bug - https://bugs.php.net/bug.php?id=64169.

akovalyov avatar Jan 17 '16 23:01 akovalyov

Same bug. PHP 5.5.9 : https://i.imgur.com/k6HPsvU.jpg

SnakeO avatar Jan 03 '17 03:01 SnakeO

Same on PHP 7.0.19 (cli) (built: May 13 2017 00:54:47) ( NTS )

@h4cc @Herzult it is connected with https://bugs.php.net/bug.php?id=73597

Solution (intval):

namespace Ssh;

class Sftp extends Subsystem 
{
.
.
.
.
    public function getUrl($filename)
    {
        return sprintf('ssh2.sftp://%s/%s', intval($this->getResource()), $filename);
    }
}

topekTopperson avatar May 24 '17 10:05 topekTopperson

Thanks for that fix @marcint1990, that worked for me.

I found I was also getting the remote fully-qualified path wrong. A bare filename would not work for me, I had to add /home/user/ as well.

Is someone raising a patch/PR for this?

halfer avatar Jul 02 '17 14:07 halfer

I'm not sure why @Herzult abandoned the repo, but fixed version can be found among forks (for example, https://github.com/GinoPane/php-ssh v1.1.2)

GinoPane avatar Feb 19 '20 13:02 GinoPane