PHP ve SFTP

Güvenli kanallardan dosya transferi için SFTP iyi bir alternatiftir.

PHP ve SFTP

Sunucunuzda php5-ssh2 eklentisinin var olduğuna emin olun.

PHP ve SFTPPHP ve SFTP

$baglanti = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($baglanti, 'kullanıcı', 'parola');

$sftp = ssh2_sftp($baglanti);

$akim = fopen("ssh2.sftp://$sftp/bir/yol/dosya", 'r');

   

$conn = ssh2_connect('example.com', 22);
ssh2_auth_pubkey_file(
    $conn,
    'username',
    '/home/username/.ssh/id_rsa.pub',
    '/home/username/.ssh/id_rsa'
);

// send a file
ssh2_scp_send($conn, '/local/filename', '/remote/filename', 0644);

// fetch file
ssh2_scp_recv($conn, '/remote/filename', '/local/filename');
 
 
$sftp = ssh2_sftp($conn);

// Create a new folder
ssh2_sftp_mkdir($sftp, '/home/username/newdir');

// Rename the folder
ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');

// Remove the new folder
ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');

// Create a symbolic link
ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');

// Remove a file
ssh2_sftp_unlink($sftp, '/home/username/myfile');

 

class SFTPConnection {

    private $connection;
    private $sftp;

    public function __construct($host, $port=22){

        if (!extension_loaded('ssh2')){
                throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }

        $this->connection = ssh2_connect($host, $port);
        if (! $this->connection)
            throw new Exception("Could not connect to $host on port $port.");
    }

    public function login($username, $password){

        if (!extension_loaded('ssh2')){
                throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }

        if (! @ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " . "and password $password.");

        $this->sftp = @ssh2_sftp($this->connection);

        if (! $this->sftp)
            throw new Exception("Could not initialize SFTP subsystem.");
    }

    public function uploadFile($local_file, $remote_file){

        if (!extension_loaded('ssh2')){
                throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }

        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

        if (! $stream)
            throw new Exception("Could not open 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($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");

        @fclose($stream);
    }
        function scanFilesystem($remote_file) {

                if (!extension_loaded('ssh2')) {
                        throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
                }

                $sftp = $this->sftp;

            $dir = "ssh2.sftp://$sftp$remote_file";

              $tempArray = array();

            $handle = opendir($dir);

          // List all the files
            while (false !== ($file = readdir($handle))) {
            if (substr("$file", 0, 1) != "."){
              if(is_dir($file)){
//                $tempArray[$file] = $this->scanFilesystem("$dir/$file");
               } else {
                 $tempArray[]=$file;
               }
             }
            }
           closedir($handle);
          return $tempArray;
        }
    public function receiveFile($remote_file, $local_file) {

        if (!extension_loaded('ssh2')){
                throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }

        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');

        if (! $stream)
            throw new Exception("Could not open file: $remote_file");
        $contents = fread($stream, filesize("ssh2.sftp://$sftp$remote_file"));

        file_put_contents ($local_file, $contents);
        @fclose($stream);
    }

    public function deleteFile($remote_file){
      if (!extension_loaded('ssh2')){
        throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
      }
      $sftp = $this->sftp;
      unlink("ssh2.sftp://$sftp$remote_file");
    }
}

 

Kaynaklar

 

 

 

Yorumunuzu Ekleyin


Yükleniyor...
Yükleniyor...