0

I recently configured phpseclib 2.0 for my server; so I'm quite new to this. When trying to upload a jpg image whose file is less than 1 MB via a normal HTML form and PHP using phpseclib, the image gets uploaded exactly where I want it to be, however with a file size of only 14 bytes (970 KB is the original file size). Any help? My code below:

// Establish the SFTP connection
$sftp = new \phpseclib\Net\SFTP('www.myhost.com');

// Login with username and password, and inform if the login was successful
if (!$sftp->login('myuser','mypassword')) {
  $uploadAnswer = 'SFTP Connection failed!';
  } else {
  $uploadAnswer = 'SFTP Connection succesful!';
  // migrate to root folder
  $rootPath = $sftp->pwd();
  // Create destination Path
  $destinationPath = $rootPath.'/myfolder/'.$_FILES['myupload']['name'];

  // Load the file onto the server, and inform if upload was successful
  if (!$sftp->put($destinationPath,$_FILES['myupload']['tmp_name'])) {
    $uploadAnswer = "There's been a problem with the Upload!";
    } else {
    $uploadAnswer = 'File successfully uploaded!';
    }
  }

When I run this, the message 'File successfully uploaded!' also gets displayed properly. What's happening here??

Joe
  • 78
  • 1
  • 7
  • Have you checked if $_FILES contained what you assumed it should to begin with? – CBroe Mar 27 '20 at 11:14
  • Tbh I don't know how to do that.. When I get the file name with $_FILES['myupload']['name']; I get the name of the uploaded file. When I echo out $_FILES['myupload']['tmp_name'] I get what looks like a randomly create path like /tmp/phprandomstrings. Does that help ya? – Joe Mar 27 '20 at 11:23
  • See [phpseclib sftp->put() command: File contents are just a string, not the expected PDF document](https://stackoverflow.com/q/47659231/850848). – Martin Prikryl Mar 27 '20 at 11:54
  • @MartinPrikryl thanks, I saw that post, but it still does not work for me if I add SFTP::SOURCE_LOCAL_FILE into the put() function as last argument. – Joe Mar 27 '20 at 11:57
  • What's also weird is that I have to define a new SFTP object using new new \phpseclib\Net\SFTP, as you can see in the code above. When I use only new SFTP, I get the error Class 'SFTP' not found. Is that how it's supposed to be?? – Joe Mar 27 '20 at 12:00
  • So show us a correct code that still does not work. Your current code is simply wrong. + Please focus on one problem per question only. + What exact version of phpseclib are you using? (edit your question, do not post information in comments). – Martin Prikryl Mar 27 '20 at 12:01

1 Answers1

1

Replacing the line

$sftp->put($destinationPath,$_FILES['myupload']['tmp_name'])

with

$sftp->put($destinationPath,$_FILES['myupload']['tmp_name'],SFTP::SOURCE_LOCAL_FILE)

As recommended by the link provided did not work properly. The solution was:

$sftp->put($destinationPath,$_FILES['myupload']['tmp_name'],\phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)

This did the job; so the problem is that the SFTP class could for some reason not be recognized properly. If anyone had the same issue already; please just let me know.

Joe
  • 78
  • 1
  • 7