note 118815 added to function.ssh2-sftp

From: Date: Wed, 10 Feb 2016 15:15:28 +0000
Subject: note 118815 added to function.ssh2-sftp
Groups: php.notes 
Request: Send a blank email to php-notes+get-204913@lists.php.net to get a copy of this message
If you wish to store once the protocol + ressource used ("ssh2.sftp://$sftp";) There's a little trick to know... This won't work : <?php function connect(){ $connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); $sftp = ssh2_sftp($connection); return "ssh2.sftp://$sftp"; } $remote = connect(); is_file( $remote."/path/to/file"); // Warning // Message: is_file(): ## is not a valid SSH2 SFTP resource ?> You need to have $sftp still avaible at moment the function (is_file, filesize, fopen, ...) uses it Otherwise, I guess the GC cleans it up and closes the ssh2_stfp connection That's why this work : <?php function connect(){ //... global $sftp; $sftp = ssh2_sftp($connection); return "ssh2.sftp://$sftp"; } $remote = connect(); is_file( $remote."/path/to/file"); // Or way better : class myClass { public function connect(){ //... $this->sftp = ssh2_sftp($connection); $this->remote = "ssh2.sftp://".$this->sftp; } public function test(){ is_file( $this->remote."/path/to/file"); } } $obj = new myClass(); $obj->connect(); $obj->test(); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 37.110.198.226 ---- Manual Page -- http://php.net/manual/en/function.ssh2-sftp.php Edit -- https://master.php.net/note/edit/118815 Del: integrated -- https://master.php.net/note/delete/118815/integrated Del: useless -- https://master.php.net/note/delete/118815/useless Del: bad code -- https://master.php.net/note/delete/118815/bad+code Del: spam -- https://master.php.net/note/delete/118815/spam Del: non-english -- https://master.php.net/note/delete/118815/non-english Del: in docs -- https://master.php.net/note/delete/118815/in+docs Del: other reasons-- https://master.php.net/note/delete/118815 Reject -- https://master.php.net/note/reject/118815 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204913) next »