using SFTP in php 5.3 with phpseclib

0

I'm trying to use phpseclib to download with php 5.3.
however 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>

However it 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?

    
asked by anonymous 18.04.2017 / 20:28

1 answer

1

Uncomment the line:

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

I think the \phpseclib\Net\SFTP is incorrect, should be something like: Net\SFTP

It works up to this order:

<?php
use Net\SFTP;

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

$sftp =  new SFTP('192.168.0.65');

Now if you are using this: link , I must tell you that this version specifically does not use namespaces yet, the correct one would be just this: / p>

<?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.

Detailing

On the namespace, it does not do include alone as in other languages, you have to use include or spl_autoload . In your case only include already resolves with spl_autoload you can do the include based on the namespace by a directory, but you have to develop this first, since spl_autoload does not work alone.

If you want to know and experience spl_autoload recommend you read this (not something to worry about now):

PSR-0 and PSR-4

18.04.2017 / 20:34