2

So what it does is successfully connects then uploads the file logo.png but the contents of the file isn't what was on web host or uploaded with html if i use a html upload code. What it puts in the file is the ACTUAL text between the second ' 's so for that very example the contents of logo.png is literally logo.png and not the picture.

require_once("ftp/vendor/autoload.php");

use phpseclib\Net\SFTP;

$sftp = new SFTP('SERVER');

if (!$sftp->login('USER', 'PW')) {
    throw new Exception('Login failed');
}

$sftp->put("/some/path/logo.png", "logo.png", NET_SFTP_LOCAL_FILE);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ryan Williams
  • 75
  • 1
  • 9

1 Answers1

4

If you would read through the documentation, you would find out that the second argument of the put() function is $data, therefore not the file path, but the actual data to write:

function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)

By default, NetSFTP::put() does not read from the local filesystem. $data is dumped directly into $remotefile. [...]

To upload a local file, the easiest way is to read the content into one variable that will be passed to the put() function:

$data = file_get_contents("logo.png");
$sftp->put("/some/path/logo.png", $data);

Edit: You are probably using a new version of phpseclib, which renamed these constants to make them more object-like. With a new version, you should use

$sftp->put("/some/path/logo.png", "logo.png", SFTP::SOURCE_LOCAL_FILE);
Community
  • 1
  • 1
Jakuje
  • 24,773
  • 12
  • 69
  • 75
  • But the `NET_SFTP_LOCAL_FILE` mode overrides the default behavior. With `NET_SFTP_LOCAL_FILE`, the `$data` argument is actually a path. So the OP's code is correct, while yours is actually wrong. – Martin Prikryl May 02 '17 at 06:48
  • It can be that the OP version does not support this functionality. – Jakuje May 02 '17 at 06:51
  • Actually `NET_SFTP_LOCAL_FILE` is supported ever since the phpseclib supports SFTP (2009): https://github.com/phpseclib/phpseclib/commit/d365b7a587afc7cb1712c4865fd48e9973a33f5d – Martin Prikryl May 02 '17 at 06:54
  • 1
    Ok, you are right, but the current version renamed the constants so it will be the opposite -- too new version, reading old tutorials and ignoring php notices. – Jakuje May 02 '17 at 06:59