PHP5 secure File Upload (sFTP)

Posted in PHP, 24.09.2014 20:09

SSH2-Connect-Methode

So funktionierts wenn SSH2 und das libssh2-php Modul installiert sind:


apt-get install libssh2-1-dev libssh2-php

 public function uploadExportFiles($files) {
        $sftp = $this->sFTPconnect();

        if (is_array($files)) {
            foreach ($files as $file) {
                $remote_file = "in/$file";
                $local_file = "../modules/marketingmaterial/export/$file";

                $sftpStream = @fopen("ssh2.sftp://$sftp/$remote_file", 'w');

                try {

                    if (!$sftpStream) {
                        throw new Exception("Could not open remote file: $remote_file");
                    }

                    $data_to_send = @file_get_contents($local_file);

                    if ($data_to_send === false) {
                        throw new Exception("Could not open local file: $local_file.");
                    }

                    if (@fwrite($sftpStream, $data_to_send) === false) {
                        throw new Exception("Could not send data from file: $local_file.");
                    }

                    fclose($sftpStream);
                } catch (Exception $e) {
                    error_log('Exception: ' . $e->getMessage());
                    fclose($sftpStream);
                }
            }
        }
        return true;
    }

    private function sFTPconnect() {

        $ftp_server = $this->config['marketingmaterial']['ftp_server'];
        $ftp_user_name = $this->config['marketingmaterial']['ftp_user_name'];
        $ftp_user_pass = $this->config['marketingmaterial']['ftp_user_pass'];

        $connection = ssh2_connect($ftp_server, 22);
        // Authenticate
        if (!ssh2_auth_password($connection, $ftp_user_name, $ftp_user_pass)) {
            throw new Exception('Unable to connect.');
        }
        // Create our SFTP resource
        if (!$sftp = ssh2_sftp($connection)) {
            throw new Exception('Unable to create SFTP connection.');
        }
        return $sftp;
    }

Andere Methode mit phpseclib

HIer erhältlich: http://phpseclib.sourceforge.net/sftp/examples.html#put


require_once($Config['system_root'] . '/Classes/phpseclib/Net/SFTP.php');
public function uploadExportFiles($files) {
global $Config;
        $path = $Config['system_root'].'/Classes/phpseclib';
        set_include_path(get_include_path() . PATH_SEPARATOR . $path);
        define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
        try {
        $sftp = new Net_SFTP($Config['marketingmaterial']['ftp_server']);
        if (!$sftp->login($Config['marketingmaterial']['ftp_user_name'], $Config['marketingmaterial']['ftp_user_pass']))
        {
            throw new Exception("Could not connect to: " .$Config['marketingmaterial']['ftp_server']);
        }
        // debug
        //$files = array('test.csv');

        if (is_array($files)) {
            foreach ($files as $file) {
                $remote_file = "in/$file";
                $local_file = __DIR__ . "/export/$file";

                    if (!$sftp->put($remote_file, $local_file, NET_SFTP_LOCAL_FILE)) {
                        echo $sftp->getSFTPLog();
                        throw new Exception("Could not upload file: $local_file");
                    }
....
     }
        }
 } catch (Exception $e) {
                    error_log('Exception: ' . $e->getMessage());
                    fclose($sftpStream);
                }