downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ssh2_shell> <ssh2_sftp_unlink
Last updated: Fri, 14 Aug 2009

view this page in

ssh2_sftp

(PECL ssh2 >= 0.9.0)

ssh2_sftpInitialise un sous-système SFTP

Description

resource ssh2_sftp ( resource $session )

Demande un sous-système SFTP depuis un serveur déjà connecté SSH2.

Liste de paramètres

session

Un identifiant de connexion SSH, obtenu depuis la fonction ssh2_connect().

Valeurs de retour

Cette méthode retourne une ressource SSH2 SFTP pour utilisation avec toutes les méthodes ssh2_sftp_*() ainsi que le gestionnaire ouvert ssh2.sftp://.

Exemples

Exemple #1 Ouverture d'un fichier 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');
?>

Voir aussi



ssh2_shell> <ssh2_sftp_unlink
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
ssh2_sftp
bas weerman
08-Jul-2008 10:13
I changed the read function to:

    public function receiveFile($remote_file, $local_file)
    {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');
        if (! $stream)
            throw new Exception("Could not open file: $remote_file");
        $size = $this->getFileSize($remote_file);           
        $contents = '';
        $read = 0;
        $len = $size;
        while ($read < $len && ($buf = fread($stream, $len - $read))) {
          $read += strlen($buf);
          $contents .= $buf;
        }       
        file_put_contents ($local_file, $contents);
        @fclose($stream);
    }

    public function getFileSize($file){
      $sftp = $this->sftp;
        return filesize("ssh2.sftp://$sftp$file");
    }
tom at r dot je
02-Jul-2008 04:33
Not sure if this is a bug of some kind of security feature.

When reading files using ssh2.sftp anything inside php tags is inexplicably stripped.

foo.php
one
<?
echo 'two';
?>
echo 'three';

$stream = fopen("ssh2.sftp://$sftp$file", 'r');
echo filesize("ssh2.sftp://$sftp$file"); //CORRECT
echo fread($stream, filesize("ssh2.sftp://$sftp$file")); //prints "onethree"

Not sure why this happens and haven't found a workaround.
bas weerman
14-May-2008 07:22
I added some functionality for scanning the filesystem and receiving and deleting files.

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);
    }
   
        function scanFilesystem($remote_file) {
              $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)
    {
        $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){
      $sftp = $this->sftp;
      unlink("ssh2.sftp://$sftp$remote_file");
    }
}
duke1 at drakkon dot net
23-Apr-2008 03:38
if anyone is interested on how to get a directory listing:
$SSH_CONNECTION= ssh2_connect('shell.example.com', 22);
ssh2_auth_password($SSH_CONNECTION, 'username', 'password');
//-------------------------------------------------------------------
//this function finds all files within  given directory and returns them
function scanFilesystem($dir) {
    $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]=scanFilesystem("$dir/$file");
        } else {
            $tempArray[]=$file;
        }
    }
    }
   closedir($handle);
  return $tempArray;
}

//-------------------------------------------------------------------
$sftp = ssh2_sftp($SSH_CONNECTION);

//code to get listing of all OUTGOING files
$dir = "ssh2.sftp://$sftp/outgoing";
$outgoing = scanFilesystem($dir);
sort($outgoing);
print_r($outgoing);
Curtis Wyatt
28-Jan-2008 03:58
The sftp class provided by David Barnes works great.  However, if you get errors about fopen and it failing to open a stream, try the fully qualified path on the remote server.

For example, if you are uploading a file to /Users/username/Sites/file.txt this may not work:

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

but this will:

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

Don't assume that since you are connecting as that user that you are starting in its home space.

Another possible option is that you need to use http://us.php.net/manual/en/function.ssh2-sftp-mkdir.php first to make the directory if it does not exist already, and then upload the file into it.
David Barnes
15-Nov-2006 11: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: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites