3

I'm using phpseclib - SFTP class and am trying to upload a file like so -

$sftp = new Net_SFTP('mydomain.com');
if (!$sftp->login('user', 'password')) {
    exit('Login Failed');
}
$sftp->put('/some-dir/',$fileTempName);

The file however isn't being uploaded inside some-dir but is uploaded one directory before (to the starting directory, lets say it's root). This is driving me crazy, I think I've tried all combinations of some-dir/ or /some-dir or /some-dir/, but the file won't upload there.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1551120
  • 627
  • 2
  • 8
  • 14
  • First Check the current working directory in SFTP. Use $sftp->pwd(). Then you will be able to provide the correct path for uploading files. Secondly, you have to provide the filepath. example, $sftp->put('/some-dir/filename_remote','localfile'); – Lawrence Gandhar Sep 30 '16 at 20:03
  • $sftp->put('/some-dir/filename_remote','localfile', NET_SFTP_LOCAL_FILE) will upload the files. Sorry for missing NET_SFTP_LOCAL_FILE in my previous comment – Lawrence Gandhar Sep 30 '16 at 20:11

1 Answers1

3

I don't think your put is doing what you think it is doing. According to the docs, you need to do a Net_SFTP::chdir('/some-dir/') to switch to the directory you want to send file to, then put($remote_file, $data), where remote_file is the name of file, and $data is the actual file data.

Example Code:

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

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

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Gary
  • 2,866
  • 1
  • 17
  • 20
  • Thanks I think that will do . So just to clarify (if anyone else has this problem) . $sftp->put first param is the file name to be created(uploaded) on the server , second param is the data (either a string text or a file handle) . If you want to upload to a specific directory use Net_SFTP::chdir('somedir') to change to that dir before you use put . – user1551120 Nov 22 '12 at 07:47