2

I need to uploaded a file to my server from a input file field and then make a copy of a folder (the file is on a subfolder of it) to another server using FTP.

I use move_uploaded_file to upload the file to my server and then I use readdir to iterate over the folder and copy the files using FTP connection, but the uploaded file isn't copy.

I write all the files get by readdir in order to know if it is a readdir issue and I found out that the file name is not printed. How can I get the file? My code is as follow:

<?php
/* Previous code here */

if ($_FILES['logo']['name'] != ""){
   move_uploaded_file($_FILES['logo']['tmp_name'], $folder. '/img/logo.png');
}

/* more code here */

function subirFTP($conn_id, $dirOrigen, $dirDestino){
    $vcarga = opendir($dirOrigen);
    while(false !== ($file = readdir($vcarga))){
        if ($file != '.' && $file != '..'){ //Si no es la carpeta raíz ni el padre
            echo "Copiando: " . $file;
            if (!is_dir($dirOrigen . $file)){ //Si es un archivo
                ftp_put($conn_id, $dirDestino . $file, $dirOrigen . $file, FTP_BINARY);
                echo " - HECHO<br>";
            }
            else{ //Si es un directorio
                echo "<br>";
                ftp_mkdir($conn_id, $dirDestino . $file);
                ftp_chdir($conn_id, $file);
                subirFTP($conn_id, $dirOrigen . $file . "/", "./");
                ftp_chdir($conn_id, '..');
            }
        }
    }
    closedir($vcarga);  
}

function copiaFTP($host, $user, $pass, $dirFTP, $codigo, $carpeta){
    // establecer una conexión básica
    $conn_id = ftp_connect($host); 

    // iniciar una sesión con nombre de usuario y contraseña
    $login_result = ftp_login($conn_id, $user, $pass); 
    ftp_pasv($conn_id, true);
    ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 3600);

    // movemos a la carpeta raiz
    ftp_chdir($conn_id, $dirFTP);

    // creamos la carpeta para el ilink
    ftp_mkdir($conn_id, $codigo);
    ftp_chdir($conn_id, $codigo);

    // copiamos los archivos
    subirFTP($conn_id, $carpeta . "/", "./");

    // cerrar la conexión ftp 
    ftp_close($conn_id);
}

$host = $_POST['ftp_host'];
$user = $_POST['ftp_user'];
$pass = $_POST['ftp_pass'];
$ftp_dir = $_POST['ftp_dir'];
copiaFTP($host, $user, $pass, $ftp_dir, $codigo, "midir");
?>

2 Answers2

0

opendir is not recursive. if you put file under img subdir you need read this directory to get your file. You read directory inside $folder variable but your moved file is under

$folder ."/img" 

you can look at example 4 of official documentation for read recursively directory

http://php.net/manual/en/function.opendir.php

ciro
  • 771
  • 1
  • 8
  • 30
  • I consider that on my code (all the code for the FTP copy is inside a function which I call recursively using the differents subfolders. I can get other files in subfolders, but the upload file is the only one I can't get. – Date Masamune Dokuganryu Jan 07 '16 at 11:16
  • can we see all code? are you sure that file is in directory ? – ciro Jan 07 '16 at 11:21
  • After the script execution I can open that file from my server. For privacity term with my company I can't show you all the code because it is part of our product, but I can show you the recursive function for the FTP copy. I edit the code above – Date Masamune Dokuganryu Jan 07 '16 at 15:22
0

First of all can you upload your file with a static Destination where all Folders are allready existing? Example:

move_uploaded_file($_FILES['logo']['tmp_name'], 'MyFolder/img/logo.png');

In my Case i had to do a lot of Tests on my Webserver with absolute URLS (123/456/myweb/public_html/img/) and relative URLs (img/).

By the way, the Upload Class (UploadHandler.php) from jQuery File Upload Plugin (https://blueimp.github.io/jQuery-File-Upload/) is very usefull when you upload Images.

lidahun
  • 11
  • 2