Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't get phpseclib to work

Tags:

php

phpseclib

What I want to do

I want to upload/download a file via sftp using php. The phpseclib-library looks very promising.

What I already did

I changed my conposer.json to:

{
    "require": {
        "nicolab/php-ftp-client":"*",
        "php-curl-class/php-curl-class":"*",
        "phpseclib/phpseclib":"*"
    }
}

Then i updated my directory. Composer installed the library in the vendor folder.

The Problem

The problem is that I am unable to create a new SFTP-Object.

Fatal error: Class 'SFTP' not found in...

I also tried NET_SFTP as classname but this didn't work also. One thing I dont understand is why on, it states that the classname is NET_SFTP when in the source its clearly called SFTP.

Also autoload seems not to work.

I added echo "testline"; to the library to see if the file is loaded. Nothing happens if I use the autoloading. if i manually include the file, "testline" is printed but the class is still not found.

My Code

Example one

<?php
include '/vendor/autoload.php';

$sftp = new SFTP('domain');
if (!$sftp->login('user', 'pass')) {
    exit('Login Failed');
}

-> no echo

Example two:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib/phpseclib');

include('Net/SSH2.php');

include('Net/SFTP.php');

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

-> echo

In both examples it cant find the class.

like image 371
Simon Balling Avatar asked Oct 27 '15 09:10

Simon Balling


1 Answers

One thing I dont understand is why on http://phpseclib.sourceforge.net/sftp/examples.html#stat it states that the classname is NET_SFTP when in the source its clearly called SFTP.

The website is documenting the 1.0 branch. The 2.0 branch has a similar API but it is not identical. In particular, it is namespaced whereas the 1.0 branch isn't.

To modify your first example...

<?php
include '/vendor/autoload.php';

$sftp = new \phpseclib\Net\SFTP('domain');
if (!$sftp->login('user', 'pass')) {
    exit('Login Failed');
}

The master branch's API is subject to change. Only 1.0 and 2.0 have fixed API's. As such I think an update to your composer.json would also be appropriate. ie. do "phpseclib/phpseclib": "~2.0" instead of *.

like image 87
neubert Avatar answered Oct 17 '22 11:10

neubert