Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

ssh2_sftp

(PECL)

ssh2_sftp --  Initialize SFTP subsystem

Description

resource ssh2_sftp ( resource session )

Request the SFTP subsystem from an already connected SSH2 server.

This method returns an SSH2 SFTP resource for use with all other ssh2_sftp_*() methods and the ssh2.sftp:// fopen wrapper.

Пример 1. Opening a file via SFTP

<?php
$connection
= ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);

$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
?>

See Also: ssh2_scp_send(), and ssh2_scp_recv()



add a note add a note User Contributed Notes
ssh2_sftp
David Barnes
15-Nov-2006 03:08
Here is an example of how to send a file with SFTP:

<?php

class SFTPConnection
{
    private
$connection;
    private
$sftp;

    public function
__construct($host, $port=22)
    {
       
$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 (! @
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)
    {
       
$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);
    }
}

try
{
   
$sftp = new SFTPConnection("localhost", 22);
   
$sftp->login("username", "password");
   
$sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
}
catch (
Exception $e)
{
    echo
$e->getMessage() . "\n";
}

?>

ssh2_shell> <ssh2_sftp_unlink
Last updated: Sat, 27 Jan 2007
 
 
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571