Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: using ssh2_sftp_realpath will make the paths relative to… #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Helper/SFTPConnection.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php namespace DnD\Bundle\MagentoConnectorBundle\Helper;/** * * @author DnD Mimosa <[email protected]> * @copyright Agence Dn'D (http://www.dnd.fr) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */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); } public function createDirectory($path) { $sftp = $this->sftp; if(is_dir("ssh2.sftp://". $sftp . $path)) return; $stream = @mkdir("ssh2.sftp://". $sftp . $path, 0777, true); if (! $stream) throw new \Exception("Could not create directory: $path"); }}
<?php namespace DnD\Bundle\MagentoConnectorBundle\Helper;/** * * @author DnD Mimosa <[email protected]> * @copyright Agence Dn'D (http://www.dnd.fr) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */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 . ssh2_sftp_realpath($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); } public function createDirectory($path) { $sftp = $this->sftp; if(is_dir("ssh2.sftp://". $sftp . ssh2_sftp_realpath($sftp,".") . $path)) return; $stream = @mkdir("ssh2.sftp://". $sftp . ssh2_sftp_realpath($sftp,".") . $path, 0777, true); if (! $stream) throw new \Exception("Could not create directory: $path"); }}
Expand Down