1

Hi i want to download csv files located on csv folder in root folder of one server to my server by using FTP connection. I added the below code . Its not working for me. FTP connection is working also all files are listed by using this command ftp_nlist.Can you please help me to download files form one folder of one server to another folder of server ?

$ftpHost   = 'ftp.site.in';
$ftpUsername = 'username';
$ftpPassword = '******';

// open an FTP connection
$connId = ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");

// try to login
if(@ftp_login($connId, $ftpUsername, $ftpPassword)){
    echo "Connected as $ftpUsername@$ftpHost";
}else{
    echo "Couldn't connect as $ftpUsername";
}
$contents = ftp_nlist($connId, ".");
var_dump($contents);
// local & server file path
$localFilePath  = 'csv';
$remoteFilePath = 'public_html/csvfiles/';
// try to download a file from server
if(ftp_get($connId, $localFilePath, $remoteFilePath, FTP_BINARY)){
    echo "File transfer successful - $localFilePath";
}else{
    echo "There was an error while downloading $localFilePath";
}
developerme
  • 1,845
  • 2
  • 15
  • 34
  • And "is not working" means what, actually? Do you get an error? Which? Do you see the application hang? Until what happens? Does the logic terminate but you see no result? Does the universe implode when you start the script? – arkascha Sep 06 '18 at 11:27
  • Most likely you have a firewall issue. Are you really sure that you know how the ftp protocol works? And that that remote server needs to be able to connect to your server? Through all firewalls? – arkascha Sep 06 '18 at 11:28
  • i getting following error There was an error while downloading csv – developerme Sep 06 '18 at 11:28
  • `ftp_get` takes a single filename as a parameter, not a directory. If you want to recursively copy down an entire directory, you'll need to loop over the listed files. You might also want to look for a library to do this for you - it'd be a lot easier than writing your own FTP commands. – iainn Sep 06 '18 at 11:30
  • i want to download only one file – developerme Sep 06 '18 at 11:41
  • This is not a Duplicate Question – developerme Sep 07 '18 at 10:38
  • You have to explain us why. From the little information you have provided so far, it is a duplicate question. – Martin Prikryl Sep 07 '18 at 12:16

1 Answers1

0

ftp_get takes paths to file, not paths to folder, in both its arguments.

So this is correct:

$localFilePath  = 'csv/myfile.csv';
$remoteFilePath = 'public_html/csvfiles/myfile.csv';

ftp_get($connId, $localFilePath, $remoteFilePath, FTP_BINARY);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992