Skip to content

Instantly share code, notes, and snippets.

@ALCales
Last active April 6, 2018 19:46
Show Gist options
  • Save ALCales/e17894eb0b108b6ccf70e7acbf40503d to your computer and use it in GitHub Desktop.
Save ALCales/e17894eb0b108b6ccf70e7acbf40503d to your computer and use it in GitHub Desktop.
Permite establecer conexión con otro servidor va SFTP y subir archivos en el
<?php
/**
* Class conexionSFTP
* ==================
* Autor: ALCales
* Permite establecer conexión con otro servidor va SFTP y subir archivos en el
*
* Ejemplo $sftp = new \SFTPConnection("127.0.0.0");
* $sftp->login("usuario", "password");*
* $sftp->subirFichero("rutaAbsolutaServidor/ficheroOriginal", "rutaAbsoultaServidorDestino/ficheroNuevo");
*
*/
class conexionSFTP {
private $conexion;
private $sftp;
/**
* SFTPConnection constructor.
*
* @param $host
* @param int $puerto
*
* @throws Exception
*/
public function __construct($host, $puerto = 22) {
$this->conexion = @ssh2_connect($host, $puerto);
if (!$this->conexion) {
throw new Exception("No se ha podido conectar al host $host por el puerto $puerto.");
}
}
/**
* Establece conexin va sftp con el servidor
*
* @param $usuario
* @param $password
*/
public function login($usuario, $password) {
if (!@ssh2_auth_password($this->conexion, $usuario, $password)) {
throw new Exception("No se ha podido autenticar con el usuario $usuario " . "y la contrasea $password.");
}
$this->sftp = @ssh2_sftp($this->conexion);
if (!$this->sftp) {
throw new Exception("No se ha podido inicializar el subsistema SFTP.");
}
}
/**
* Copia el fichero local en el servidor remoto en la ruta destino dada
*
* @param $ficheroLocal
* @param $ficheroRemoto
*/
public function subirFichero($ficheroLocal, $ficheroRemoto) {
$sftp = intval($this->sftp);
$stream = @fopen("ssh2.sftp://$sftp$ficheroRemoto", 'w');
if (!$stream) {
throw new Exception("No se ha podido abrir el fichero remoto: $ficheroRemoto");
}
$datosFicheroLocal = @file_get_contents($ficheroLocal);
if ($datosFicheroLocal === false) {
throw new Exception("No se ha podido leer el fichero local: $ficheroLocal.");
}
if (@fwrite($stream, $datosFicheroLocal) === false) {
throw new Exception("No se han podido enviar los datos desde el fichero local: $ficheroLocal.");
}
@fclose($stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment