Skip to content

kotomono7/php_ssh2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

PHP SSH2

About

This is a simple function to connect with sftp server using php and ssh2 lib.
What you can do with this function:

  • Connect and login to sftp server
  • Read file (.txt)
  • Read file (.csv) and convert to arrays
  • Get filesize and scan folder
  • Upload file to sftp server
  • Receive (download) file from sftp server
  • Delete file on sftp server

Requirements

PHP >= 5, libssh2-1, libssh2-1-dev, libssh2-php, pecl ssh2 (https://pecl.php.net/package/ssh2)

How To

<?php

include("ftp_class.php");

try {
      // connect and login to sftp server
      $sftp = new SFTPConnection("ip_address_or_domain_name", port);
      $sftp->login("username", "password");
      echo "Connected to sftp server.";  
      
      // read file on sftp server
      $data = $sftp->readFile("/home/username/file.txt");
      echo '<pre>';
      print_r($data);
      echo '</pre>';
      
      // read .csv file on sftp server and convert it to arrays
      $csv = $sftp->readCSV("/home/username/data.csv");
      echo '<pre>';
      print_r($csv);
      echo '</pre>';
      
      // scan folder on sftp server
      $scan = $sftp->scanFilesystem("/folder_name");
      echo '<pre>';
      print_r($scan);
      echo '</pre>';
} catch (Exception $e) {
      echo $e->getMessage()."\n";
}
  
?>

Voila!!!

About

Connect to SFTP server using php ssh2

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages