using SFTP in php 5.3 with phpseclib

Asked

Viewed 273 times

0

I’m trying to use the phpseclib to download with php 5.3.
yet I’m not getting it to work.

my test is this code:

    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <?php
               // include './phpseclib/Net/SFTP.php'; 
               use \phpseclib\Net\SFTP;

               $sftp =  new SFTP('192.168.0.65');

               if (!$sftp->login('oracle', '142536')) {
                     exit('Login Failed');
                }


               echo $sftp->pwd() . "\r\n";
               $sftp->put( '/home/oracle/teste.txt','./teste.txt',$sftp::SOURCE_LOCAL_FILE);
               $texto = $sftp->get('/home/oracle/teste.txt');
               echo $texto;

            ?>
        </body>
    </html>

Yet always returns :

        ( ! ) Fatal error: Class 'phpseclib\Net\SFTP' not found in C:\workspace\ssh_php\index.php on line 17
        Call Stack
        #   Time    Memory  Function    Location
        1   0.1068  127552  {main}( )   ..\index.php:0

How do I recognize the namespace in a structured code?

1 answer

1


Remove the comment from the line:

// include './phpseclib/Net/SFTP.php';

I believe that the \phpseclib\Net\SFTP be incorrect, should be something like: Net\SFTP

Works even in this order:

<?php
use Net\SFTP;

include './phpseclib/Net/SFTP.php';

$sftp =  new SFTP('192.168.0.65');

Now if you’re using this: http://phpseclib.sourceforge.net, I must tell you that this version specifically does not use namespaces yet, the correct would be this only:

<?php

include './phpseclib/Net/SFTP.php';

$sftp = new Net_SFTP('192.168.0.65');

The code is old and was written before PHP supports namespaces.

Details

About namespace, it does not include itself as in other languages, it has to use include or spl_autoload. In your case only the include already resolved with the spl_autoload you can do include by basing the namespace by a directory, but you need to develop this first, because the spl_autoload does not work alone.

If you want to meet and experience the spl_autoload I recommend you read this (it’s not something to worry about now):

PSR-0 and PSR-4

  • 1

    Using include didn’t work because dependencies were asking to be included. I researched about this autoload and got this function that solved me spl_autoload_register(Function($class_name) { include $class_name . '. php'; });

  • @Hagahood, where did you get this lib? I thought they didn’t use namespace.

  • github https://github.com/phpseclib/phpseclib

  • @Cool hagahood, are using namespaces in this version, should be a Fork.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.