<?php
/**
* Function returns contents via FTP connection and returns it as string (right version...)
*/
function ftp_get_contents ($conn_id, $filename) {
//Create temp handler:
$tempHandle = fopen('php://temp', 'r+');
//Get file from FTP:
if (@ftp_fget($conn_id, $tempHandle, $filename, FTP_ASCII, 0)) {
rewind($tempHandle);
return stream_get_contents($tempHandle);
} else {
return false;
}
}
?>
ftp_fget
(PHP 4, PHP 5)
ftp_fget — Descarga un archivo desde el servidor FTP y lo guarda en un archivo abierto
Descripción
bool ftp_fget
( resource $secuencia_ftp
, resource $gestor
, string $archivo_remoto
, int $modo
[, int $pos_reanudar
] )
ftp_fget() recupera archivo_remoto desde el servidor FTP, y lo escribe en el apuntador de archivo dado.
Lista de parámetros
- secuencia_ftp
-
El identificador de enlace de la conexión FTP.
- gestor
-
Un apuntador de archivo abierto en el que se almacenan los datos.
- archivo_remoto
-
La ruta del archivo remoto.
- modo
-
El modo de transferencia. Debe ser FTP_ASCII o FTP_BINARY.
- pos_reanudar
-
La posición desde la cual se empieza a descargar el archivo remoto.
Valores retornados
Devuelve TRUE si todo se llevó a cabo correctamente, FALSE en caso de fallo.
Ejemplos
Example #1 Ejemplo de ftp_fget()
<?php
// ruta al archivo remoto
$archivo_remoto = 'algun_archivo.txt';
$archivo_local = 'archivo_local.txt';
// abrir algún archivo para escritura
$gestor = fopen($archivo_local, 'w');
// establecer la conexión básica
$id_con = ftp_connect($servidor_ftp);
// iniciar sesión con nombre de usuario y contraseña
$resultado_login = ftp_login($id_con, $ftp_nombre_usuario, $ftp_contrasenya);
// intento de descargar $archivo_remoto y guardarlo en $gestor
if (ftp_fget($id_con, $gestor, $archivo_remoto, FTP_ASCII, 0)) {
echo "Se ha escrito satisfactoriamente sobre $archivo_local\n";
} else {
echo "Ha ocurrido un error mientras se descargaba $archivo_remoto en $archivo_local\n";
}
// cerrar la conexión y el gestor de archivo
ftp_close($id_con);
fclose($gestor);
?>
Registro de cambios
| Versión | Descripción |
|---|---|
| 4.3.0 | Se agregó pos_reanudar . |
Ver también
- ftp_get() - Descarga un archivo desde el servidor FTP
- ftp_nb_get() - Recupera un archivo desde el servidor FTP y lo escribe sobre un archivo local (modo no-bloqueo)
- ftp_nb_fget() - Recupera un archivo desde el servidor FTP y lo escribe sobre un archivo abierto (modo no-bloqueo)
ftp_fget
broom at alturnanetworks dot com
03-Oct-2008 11:49
03-Oct-2008 11:49
broom at alturnanetworks dot com
03-Oct-2008 11:18
03-Oct-2008 11:18
Another ftp_get_contents approach, using a temperary stream handler. Returns file contents as string.
<?php
function ftp_get_contents ($conn_id, $filename,
//Create temp handler:
$tempHandle = fopen('php://temp', 'r+');
//Get file from FTP assuming that it exists:
ftp_fget($conn_id, $tempHandle, $filename, FTP_ASCII, 0));
//Getting detailed stats to check filesize:
$fstats = fstat($tempHandle);
return fread($tempHandle, $fstats['size']);
}
?>
justrafi at gmail dot com
06-Feb-2008 04:44
06-Feb-2008 04:44
I was in need to synchronize two folders on two separate servers, one is a Windows server, and the other is a Linux server. I created this short and sweet function to help me do this. PLEASE NOTICE: This will not copy folders, and probably will fail if remote folder contains anything else than files.
function sync_folders($host, $port, $username, $password, $remote_dir, $local_dir, $passive_mode = true) {
$conn_id = ftp_connect($host, $port);
if (!$conn_id) return false; # fail to connect
if (!ftp_login($conn_id, $username, $password)) { ftp_close($conn_id); return false; } # fail to login
ftp_pasv($conn_id, $passive_mode);
if (!ftp_chdir($conn_id, $remote_dir)) { ftp_close($conn_id); return false; } # fail to change dir
if (substr($local_dir, -1) != '/') $local_dir .= '/';
$list = ftp_nlist($conn_id, '.');
sort($list);
foreach ($list as $file) {
if (!file_exists($local_dir . $file)) {
$is_copied = ftp_get($conn_id, $local_dir . $file, $file, FTP_BINARY);
}
}
ftp_close($conn_id);
return true;
}
rodrigo-rocha at oi dot net dot br
06-Sep-2002 02:56
06-Sep-2002 02:56
If you suply only a filename to the second parameter of function the ftp_get will open a pointer to the local file creating it and write to it.It's ok if your server dont execute for to mutch time and you dont get too many files but if you do it too many times the pointers created by ftp_get will not be closed and will end your opened files capacity at your server making it to do not open any more files until you restart it.
