You want to make sure you check the existence of the new name before renaming files, because otherwise you could risk losing files. Just do a simple check with ftp_size with the new name. If it's !=-1, you're going to want to throw some kind of error, otherwise you'll be losing a file...
ftp_rename
(PHP 4, PHP 5)
ftp_rename — Renombra un archivo o un directorio en el servidor FTP
Descripción
bool ftp_rename
( resource $secuencia_ftp
, string $nombre_viejo
, string $nombre_nuevo
)
ftp_rename() renombra un archivo o un directorio en el servidor FTP.
Lista de parámetros
- secuencia_ftp
-
El identificador de enlace de la conexión FTP.
- nombre_viejo
-
El nombre del archivo/directorio viejo.
- nombre_nuevo
-
El nuevo nombre.
Valores retornados
Devuelve TRUE si todo se llevó a cabo correctamente, FALSE en caso de fallo.
Ejemplos
Example #1 Ejemplo de ftp_rename()
<?php
$archivo_viejo = 'un_archivo.txt.bak';
$archivo_nuevo = 'un_archivo.txt';
// establecer la conexión básica
$id_con = ftp_connect($ftp_server);
// iniciar sesión con nombre de usuario y contraseña
$resultado_login = ftp_login($id_con, $ftp_nombre_usuario, $ftp_contrasenya);
// tratar de renombrar $archivo_viejo a $archivo_nuevo
if (ftp_rename($id_con, $archivo_viejo, $archivo_nuevo)) {
echo "se ha renombrado $archivo_viejo a $archivo_nuevo con éxito\n";
} else {
echo "Hubo un problema al renombrar $archivo_viejo a $archivo_nuevo\n";
}
// cerrar la conexión
ftp_close($id_con);
?>
ftp_rename
alishahnovin at hotmail dot com
11-Dec-2007 07:25
11-Dec-2007 07:25
aventaria at hotmxxx dot com
07-Feb-2007 04:51
07-Feb-2007 04:51
This function isn't only able to rename files, but also folders. And it is not only able to rename them, but also move them, and, in the case of folders, their contents as well (so folders don't have to be empty to move). For example:
<?php
ftp_rename($conn_id, "./dir1/dir2/", "./dir3/");
?>
Now the folder dir2 (which prevously was in folder dir1) has moved to the same folder as dir1, and it has kept its original contents (the content just moved along).
Hazem dot Khaled at gmail dot com
26-Nov-2006 07:56
26-Nov-2006 07:56
to rename the file or folder you should use ftp_chdir to select the current directory on ftp server or you should write the full path to file in old name and in new name
Ex. 1
<?php
// open the folder that have the file
ftp_chdir($conn_id, '/www/ftp-filemanager/');
// rename the file
ftp_rename($conn_id, 'file1.jpg', 'new_name_4_file.jpg');
?>
or write full path Ex. 2
<?
// rename the file
ftp_rename($conn_id, '/www/ftp-filemanager/file1.jpg', '/www/ftp-filemanager/new_name_4_file.jpg');
?>
Hugo locobyte at hotmail dot NO_SPAM dot com
22-Nov-2002 12:29
22-Nov-2002 12:29
Using "ftp_rename" to move files to other directories on server ftp
..
...
if(ftp_rename($conn_ftp, $xfiles[$i], "./dirx/".$xfiles[$i])) {
echo "File $xfiles[$i] moved to ./dirx";
} else {
echo "ERROR!!!. The file could not be moved";
}
...
..
#-->>h2m, bye
