Created
June 15, 2012 10:41
-
-
Save BenjaminGuV/2935795 to your computer and use it in GitHub Desktop.
Clase FTP en php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* clase de FTP | |
*/ | |
class Ftp | |
{ | |
private $server; | |
private $usuario; | |
private $password; | |
private $mode; | |
private $conexion; | |
private $messageArray = array(); | |
private $loginOk = false; | |
function __construct() | |
{ | |
} | |
private function logMessage( $message ) | |
{ | |
return $this->messageArray[] = $message; | |
} | |
public function getMessages() | |
{ | |
return $this->messageArray; | |
} | |
public function leerDirectorioFuente($ruta, $banArch = false){ | |
if($dir = opendir($ruta)){ | |
chdir($ruta); | |
//Creamos los array de archivos y carpetas | |
$archivos=array(); | |
$carpetas=array(); | |
//Leemos todos los elementos del directorio | |
while (($leerDir = readdir($dir)) !== false){ | |
if(!is_dir ($leerDir)) array_push($archivos,$leerDir); | |
else array_push($carpetas,$leerDir); | |
} | |
//Mostrar carpetas | |
foreach($carpetas as $aux){ | |
if ( $aux == '.' || $aux == '..' ) { | |
# nada | |
} else { | |
$new[] = $aux; | |
} | |
} | |
//Mostrar Archivos | |
if ( $banArch ) { | |
foreach($archivos as $aux){ | |
$new[] .= $aux; | |
} | |
} | |
//Cerrar directorio | |
closedir($dir); | |
} | |
return array($new); | |
} | |
public function conectar($server, $usuario, $password, $con_pasivo = false) | |
{ | |
$this->server = $server; | |
$this->usuario = $usuario; | |
$this->password = $password; | |
$this->conexion = ftp_connect( $this->server ); | |
$login = ftp_login( $this->conexion, $this->usuario, $this->password); | |
ftp_pasv( $this->conexion, $con_pasivo); | |
if (!$this->conexion || !$login) { | |
$this->logMessage('La conexion FTP ha fallado'); | |
}else{ | |
$this->logMessage('Conexion exitosa'); | |
$this->loginOk = true; | |
} | |
return true; | |
} | |
public function subirDirectorios( $destino, $fuente, $mode = FTP_BINARY ){ | |
$upload = ftp_put($this->conexion, $destino, $fuente, $mode); | |
if (!$upload) { | |
$this->logMessage('La subida ha fallado con el archivo ' . $fuente . ' ***'); | |
$temp = false; | |
}else{ | |
$this->logMessage('Up: ' . $fuente . ' --> ' . $destino . ' done' ); | |
$temp = true; | |
} | |
return $temp; | |
} | |
public function crearDirectorio( $carpeta ) | |
{ | |
if (ftp_mkdir( $this->conexion , $carpeta)) { | |
$this->logMessage( 'El directorio ' . $carpeta . ' se ha creado' ); | |
$temp = true; | |
} else { | |
$this->logMessage('No se pudo crear el directorio ' . $carpeta ); | |
$temp = false; | |
} | |
return $temp; | |
} | |
public function cargandoCarpeta( $carpeta ) | |
{ | |
if ( ftp_chdir( $this->conexion, $carpeta) ) { | |
$this->logMessage( 'Se esta cargando el contenido...' ); | |
$temp = true; | |
} else { | |
$this->logMessage( 'No se pudo cargar el contenido' ); | |
$temp = false; | |
} | |
return $temp; | |
} | |
public function getListaContenido( $carpeta = '.', $parametro = '-la') | |
{ | |
$contenidoArray = ftp_nlist($this->conexion, $parametro . ' ' . $carpeta ); | |
return $contenidoArray; | |
} | |
public function descargar( $fuente, $destino, $modo = FTP_BINARY ) | |
{ | |
if ( ftp_get($this->conexion, $destino, $fuente, $modo, 0) ) { | |
$this->logMessage( 'Se ha descargado el archivo: ' . $destino ); | |
$temp = true; | |
} else { | |
$this->logMessage( 'Ha fallado la descarga: ' . $destino ); | |
$temp = false; | |
} | |
return $temp; | |
} | |
public function desconectar() | |
{ | |
if ( $this->conexion ) { | |
$this->logMessage( 'Se ha desconectado del FTP' ); | |
ftp_close( $this->conexion ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment