Failed to pass files from VPS to a shared FTP hosting

1

The destination FTP folder has permissions 775, and the FTP user is the administrator user of that shared hosting account and therefore I understand that the permissions to be made The transfer is adequate to write.

The code includes a function that recursively copies the structure of the complete folder, with subfolders, etc ... and this does comply, that is, the folder structure generates it, so I have doubts that the problem is of permissions, but the files never copy them.

The code is simple, but always gives an error:

$ftphost = "ftp.destino.com";
$ftpuser = "usuario-ftp";
$ftppass = "pass-ftp";

$conexion = ftp_connect($ftphost);
$ftplogin = ftp_login($conexion, $ftpuser, $ftppass) or die ("Error FTP login");

$origen = "/ftp.php"; //Esto genera la ruta /home/xxxxx/web/dominio/test/ftp.php

//Comprobado que en destino la ruta actual es la que debe ser.
$destino = "/ftp.movido.php";

if(ftp_put($conexion, $destino, $origen, FTP_BINARY))
    echo "OK";
else
    echo "ERROR. <br>origen: $origen<br>destino: $destino";

if(ftp_close($conexion))
    echo "<br><br>OK: Conexión FTP cerrada<br>";
else
    "<br><br>ERROR: No se ha podido cerrar la conexión FTP<br>"; 

Note: For testing I have also tried with FTP_ASCII mode but the result is the same.

    
asked by Vera Canet 23.03.2017 в 12:52
source

2 answers

3

Solved.

The problem was that after making the ftp_login () it is necessary to indicate to the connection the transfer mode, in this case PASV. Therefore the problem has been resolved by adding

ftp_pasv($conexion, true);

right after the ftp_login.

    
answered by 23.03.2017 / 13:17
source
1

I leave you these two scripts I had in my code library :).

One is to import, another to export. As is logical, to import you must use it from the shared hosting, to export you must run it from the VPS server.

Either one should work without problem. Change what you need: file names, user names, passwords, etc.

Import

/**
 * Transferir (Importar) Archivos de Servidor a Servidor usando PHP FTP
 */

/* Archivo origen y ruta si necesario */
$remote_file = 'archivo.zip';

/* Cuenta FTP  */
$ftp_host = 'your-ftp-host.com'; /* host */
$ftp_user_name = '[email protected]'; /* nombre usuario */
$ftp_user_pass = 'ftppassword'; /* password */


/* Nuevo nombre de archivo para el archivo que se importará */
$local_file = 'archivo.zip';

/* Conectar usando FTP */
$connect_it = ftp_connect( $ftp_host );

/* Login a FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );

/* Descargar $remote_file y guardar a $local_file */
if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
    echo "El archivo $local_file fue transferido con éxito!\n";
}
else {
    echo "Ha habido un error\n";
}

/* Cerrar conexión */
ftp_close( $connect_it );

Export

/**
 * Transferir (Exportar) Archivos de Servidor a Servidor PHP FTP
 */

/* Nombre de archivo remoto y ruta */
$remote_file = 'files.zip';

/* Cuenta FTP  (Servidor Remoto) */
$ftp_host = 'your-ftp-host.com'; /* host */
$ftp_user_name = '[email protected]'; /* username */
$ftp_user_pass = 'ftppassword'; /* password */


/* Archivo y Ruta para enviar al Servidor FTP remoto */
$local_file = 'files.zip';

/* Conectar a FTP */
$connect_it = ftp_connect( $ftp_host );

/* Login a FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );

/* Enviar $local_file a FTP */
if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
    echo "El archivo $local_file fue transferido con éxito!\n";
}
else {
    echo "Hubo un error\n";
}

/* Cerrar conexión */
ftp_close( $connect_it );
    
answered by 23.03.2017 в 13:18