'FTP_get : failed to open stream Is a directory in /usr/local/bin

I am trying to get yesterday csv files from FTP, I have multiple folders which have multiple csv files, the FTP folder structure is below, I can find the csv file from each folder and print it but not able to get the file to local server.

FTP folder structure:

/home/lan1/city/hyd/2022-03-14/hyd.csv
/home/lan1/city/knr/2022-03-14/knr.csv
/home/lan1/city/wgl/2022-03-14/wgl.csv
    $login_result = ftp_login($con, $server['stager_usr'], $server['stager_pwd']);
        ftp_pasv($con, true);
        
        $now = time();
        $yesterday = $now - (24 * 60 * 60);
        $date = date("Y-m-d", $yesterday);
        if (isset($_GET['date'])) {
            $date = $_GET['date'];
        }
        
        $date_dir = strftime("/%Y-%m-%d", $yesterday);
        $date_dir = $server['basedir'].$date_dir;
        
        $path =  $server['basedir'];
        
        $serverDir = $_SERVER['DOCUMENT_ROOT'];
        $imagesTop = "/CSV";
        
        
        $startDate = "$date 00:00:00";
        $endDate = "$date 23:59:59";
        
        $files = ftp_nlist($con,$date_dir);
        $date_dir =  strftime("%Y-%m-%d", $yesterday);
        $date_dir = $path . $date_dir;
          foreach ($files as  $file)
         {
                if (preg_match("/\.csv$/i", $file))
                {
                    echo"\n  Found $file \n";
                      ftp_get($con, $serverDir, $file, FTP_BINARY);
                   }
        
              }
        }
ftp_close($con); 
}
}

I am not able to download the files from ftp. I have full permission on the folder. I am not able to understand how to provide the path.



Solution 1:[1]

ftp_get() takes a file path for the second parameter, not just a directory. Try something like this:

ftp_get($con, $serverDir.'/'.basename($file), $file, FTP_BINARY);

Also be careful if ftp_nlist() returns full paths or just filenames (it may depend on the server).

Solution 2:[2]

you can try this

ftp_get($con, $serverDir.basename($file), $file, FTP_BINARY);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olivier
Solution 2 sivan