PHP and SSH2 Class – Easily put/get files
I wrote a short little helper class for retrieving files using the ssh2 library w/PHP. Don’t forget to enable/install it to your PHP installation!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
<?php class SFTP { private $_server; private $_username; private $_password; private $_resource; function __construct($server = '', $username = '', $password = '') { $this->_server = $server; $this->_username = $username; $this->_password = $password; $this->_resource = false; try { $this->OpenConnection(); } catch ( Exception $e ) { throw new Exception('Unable to connect to SFTP server! Error:' . $e->getMessage()); } } private function OpenConnection() { try { $this->_resource = ssh2_connect($this->_server); } catch ( Exception $e ) { throw new Exception('Unable to ssh2_connect to $this->_server Error: ' . $e->getMessage()); } // authenticate if ( $this->_resource !== false ) { try { if ( ssh2_auth_password($this->_resource, $this->_username, $this->_password) ) { return true; } } catch ( Exception $e ) { throw new Exception('Unable to ssh2_auth_password to $this->_server Error: ' . $e->getMessage()); } } throw new Exception('Unable to authentication to server successfully'); } private function CreateSession() { // TODO: Error checking return ssh2_sftp($this->_resource); } public function PutFile($source_file, $destination_file) { try { $sftp = $this->CreateSession(); $sftpStream = fopen('ssh2.sftp://'.$sftp.$destination_file, 'w'); if (!$sftpStream) { throw new Exception("Could not open remote file: $destination_file"); } $data_to_send = file_get_contents($source_file); if ($data_to_send === false) { throw new Exception("Could not open local file: $source_file."); } if (fwrite($sftpStream, $data_to_send) === false) { throw new Exception("Could not send data from file: $source_file."); } echo "File uploaded successfully: " . basename($source_file) . "\r\n"; fclose($sftpStream); } catch (Exception $e) { fclose($sftpStream); throw new Exception('Exception: ' . $e->getMessage()); } return true; } public function GetFileList() { $files = array(); try { $sftp = $this->CreateSession(); $dirHandle = opendir("ssh2.sftp://$sftp/"); // Properly scan through the directory for files, ignoring directory indexes (. & ..) while (false !== ($file = readdir($dirHandle))) { if ($file != '.' && $file != '..') { $files[] = $file; } } } catch ( Exception $e ) { throw new Exception('Unable to retrieve file list from server: ' . $e->getMessage()); } return $files; } public function GetFile($fileName) { try { $sftp = $this->CreateSession(); // Remote stream if (!$remoteStream = @fopen("ssh2.sftp://$sftp/$fileName", 'r')) { throw new Exception("Unable to open remote file: $fileName"); } // Get our file from the remote stream $contents = ''; $read = 0; $fileSize = filesize("ssh2.sftp://$sftp/$fileName"); while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) { // Increase our bytes read $read += strlen($buffer); $contents .= $buffer; } // Close our stream fclose($remoteStream); return $buffer; } catch ( Exception $e ) { throw new Exception($e->getMessage()); } return NULL; } } ?> |
Credits to Random Drake for the listing/downloading code