skip to Main Content

I am using below code to transfer files to an SFTP server

use phpseclib3NetSFTP;

$sftp = new SFTP('localhost');

$sftp->login('user', 'password');
error_log($sftp->pwd());

foreach ($fileList as $key => $value) {
    $output = $sftp->put("//data//myfile.txt, //sourceFile.txt, SFTP::SOURCE_LOCAL_FILE);
    error_log($output);
}

error_log($sftp->getLastError());
// error_log($sftp->getSFTPLastError());
error_log('------------------------------------');
error_log("<pre>" . print_r($sftp->getErrors(), true) . "</pre>");
// error_log("<pre>" . print_r($sftp->getSFTPErrors(), true) . "</pre>");
error_log("<pre>" . print_r($sftp->getLog(), true) . "</pre>");
error_log("<pre>" . print_r($sftp->getSFTPLog(), true) . "</pre>");

which works fine. The only issue I have is that it is not throwing any error messages if it fails (only $output becomes null).
What do I need to change to get proper logging messages or at least responses from the SFTP server in case of any issues?

2

Answers


  1. Chosen as BEST ANSWER

    copying @neubert comment here to show the correct answer to my question:

    you'd need to do define('NET_SFTP_LOGGING', 2) at the top of the file for that to work.


  2. You can activate logging in phpseclib using the enableLog() method. Additionally, set the logIdent property to distinguish the source of the log entries.

    use phpseclib3NetSFTP;
    
    // Define a callback function for logging
    function customLogger($message)
    {
        error_log($message);
    }
    
    // Enable logging and set the log callback function
    SFTP::enableLog('customLogger');
    SFTP::$logIdent = 'MySFTPClient';
    
    $sftp = new SFTP('localhost');
    if (!$sftp->login('user', 'password')) {
        error_log('Login failed');
        exit;
    }
    error_log('Current directory: ' . $sftp->pwd());
    
    foreach ($fileList as $key => $value) {
        $output = $sftp->put('//data//myfile.txt', '//sourceFile.txt', SFTP::SOURCE_LOCAL_FILE);
        error_log('Put operation result: ' . var_export($output, true));
    }
    
    error_log('Last SFTP error: ' . $sftp->getLastError());
    error_log('phpseclib errors: ' . print_r($sftp->getErrors(), true));
    error_log('SFTP errors: ' . print_r($sftp->getSFTPErrors(), true));
    error_log('phpseclib logs: ' . print_r($sftp->getLog(), true));
    error_log('SFTP logs: ' . print_r($sftp->getSFTPLog(), true));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search