Hi Fellows,

I am having trouble sending a file to SFTP and error coming in line 14

Warning: ssh2_scp_send(): Failure creating remote file: (null) in path on line 14

fyi, i have read write permissions to that sftp folder

 
<?php

$server = 'sftp.my.com';
$port = '22';
$username = 'myLogin';
$passwd = 'Pa$$w0rd';
$localFile = '000001.txt';


$connection = ssh2_connect($server, $port);
if (ssh2_auth_password($connection, $username, $passwd)) {
echo "Connection successful, uploading file now...</br>";
$sftp = ssh2_sftp($connection);
$sftp = ssh2_scp_send($connection, "000001.txt", "/upload/".$localFile, 0644);
if($sftp){
echo"<h1>Success</h1>";
return ":)";
}

    Is the target "upload" directory actually at the root level on that server's file system, or should it be relative to that user's home directory?

      It is at the root level...hope image will help

      upld.PNG

        I'm no expert on this, but I know that s?ftp can be configured (not sure if by default or not?) that the ftp user only has access to directories under their home directory, or any other directories specifically defined as accessible in the server ftp config. This might be easy to confirm in your case by connecting with a tool like Filezilla using your host/user connection params, and then seeing if you can even navigate to that directory.

        PS: Or is that what your image was already showing? (In which case, never mind 🙁 )

          Geek4;11064793 wrote:

          It is at the root level...hope image will help

          If you're in a Jail, a Docker container, or any type of CHROOTed environment, your "root" might not be the same as PHP's.

          Have you tried omitting the beginning slash?

          $sftp = ssh2_scp_send($connection, "000001.txt", "upload/".$localFile, 0644); 

          Alternatively, try the "curdir" placeholder:

          $sftp = ssh2_scp_send($connection, "000001.txt", "./upload/".$localFile, 0644); 

          As a matter of fact is MAY be possible that your version of PHP needs the absolute path on the remote system, so you can (probably should) use [man]ssh2_sftp_realpath/man to determine the real path. I've seen some references that seem to suggest you must use real paths with ssh2_scp_send()....

            I tried curdir placeholder too, but still got same error. Also did real path with fopen and fclose statement, no success yet :bemused:

              Write a Reply...