Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
download all csv files from ftp recursive

I am trying to get the csv files from from different folders on FTP recursiverly, but getting an error while trying to download, I searched most of the questions but not releavent to the situation where I am struck.

FTP: csv file location
/home/lanein1/ftpfiles/AIN/2021-07-14/AIN.csv
/home/lanein1/ftpfiles/AOUT/2021-07-14/AOUT.csv
/home/lanein1/ftpfiles/BIN/2021-07-14/BIN.csv
/home/lanein1/ftpfiles/AOUT/2021-07-14/BOUT.csv

I can get to the directory ftpfiles and do a pregmatch , but I am getting Invalid argument supplied foreach()

Code:

$login_result = ftp_login($con, usr,pwd);
 ftp_pasv($con, true);
 basedir = /home/lanein1/ftpfiles/ --- this is were i am stuck to pass the   different folders
 $path =  'basedir'.date('Y.m.d',strtotime("-1 days"));
 $files = ftp_nlist($con,$path);
   {
   foreach ($files as  $file)
        { 
        if (preg_match("/\.csv$/i", $file))
        {
            echo" Found $file\n";
          ftp_get($con, $file, FTP_BINARY);
        }
        else
       {
        echo "not Found";
    
      }
}    
 ftp_close($con);
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You should try one more for each loop. I've written the below script based on the description. It might not be perfect as I did not test anything. You may need to change it a little bit :)

$login_result = ftp_login($con, $usr,$pwd);
ftp_pasv($con, true);
$path_to_ftpfiles = "/home/lanein1/ftpfiles/";
$date = date('Y-m-d',strtotime("-1 days")); // correct date format
$files = ftp_mlsd($con, $path_to_ftpfiles); // use ftp_mlsd
$supported_types = [
            'text/plain',
            'application/csv',
            'application/x-csv',
            'text/csv',
            'text/plain',            
        ];
if(!empty($files)){ // check if files exist
    foreach ($files as $file){ 
        if ($file["type"] == "dir"){ // check if it's directory               
            $child_folder_files = ftp_mlsd($con, "$path_to_ftpfiles/{$file["name"]}/$date/"); // get child dir files
            if(!empty($child_folder_files)){ // check if files exist inside child directory
                foreach ($child_folder_files as $child_file){ 
                    if ($child_file["type"] != "dir" && in_array(mime_content_type($child_file),$supported_types){     
                      $random_number = random_int(100000, 999999); // generate 6 digit random number if multiple files present in folder
                      $local_file = "{$child_file['name']}_$random_number.csv";  
                      $server_file = $child_file;                     
                      ftp_get($con, $local_file, $server_file, FTP_ASCII); // get csv
                    }
                }
            }
        }
    } 
}

You can bypass mime_content_type check if only CSV files present inside the date folder

if(!empty($child_folder_files)){ // check if files exist inside child directory
    foreach ($child_folder_files as $child_file){ 
        if ($child_file["type"] != "dir"){     
          $random_number = random_int(100000, 999999); // generate 6 digit random number if multiple files present in folder
          $local_file = "{$child_file['name']}_$random_number.csv"; 
          $server_file = $child_file;                     
          ftp_get($con, $local_file, $server_file, FTP_ASCII); // get csv
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

I checked loading for my ftp and js files - it works correctly. I had created static\js in my Desktop before run code.

$basedir = '/static'; // I setted path as string;
$path = $basedir.'/js';

$files = ftp_nlist($con, $path);
foreach ($files as $file)
{ 
    if (preg_match("/\.js$/i", $file))
    {
      echo" Found $file\n";
      ftp_get($con, '~/home/daniil/Desktop'. $file, $file);
    }
    else
    {
      echo "other file\n"; // found but it doesn't match
    }
}   

according to documentation:

 ftp_get(
    resource $ftp,
    string $local_filename,  <--- both are needed
    string $remote_filename, <---
    int $mode = FTP_BINARY,  <--- default value
    int $offset = 0
): bool
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda