Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused using ftp_get in php

Tags:

php

get

ftp

Im confused by an estate agency vebra import script that worked fine before it was moved to a different server however now this seems to not work at all.

Does anyone have any idea why i would get the following warnings...

Warning: ftp_get() [function.ftp-get]: Opening data channel for file transfer. in /home/username/public_html/includes/cron/import/vebra-import.php on line 37

Warning: ftp_get() [function.ftp-get]: Transfer OK in /home/username/public_html/includes/cron/import/vebra-import.php on line 37

here is the ftp connection code:

  $ftp = ftp_connect($ftp_host, 21) or die("FTP Connection Error");

  ftp_login($ftp, $ftp_user, $ftp_pass) or die("Can't Connect to FTP");

  $ftpdir = ftp_nlist($ftp, "/");


  if(!empty($ftpdir) && count($ftpdir) > 0) {

  foreach($ftpdir as $ftpfile) {

      if(preg_match("/\.txt$/", $ftpfile)) {
          $getfile = ftp_get($ftp, $csv_dir.$ftpfile, $ftpfile, FTP_BINARY);
          if($getfile){
              $downloaded++;
          }
          $total++;

      }

  }

  }
  ftp_close($ftp);

Furthermore, it seems to be intermittent, and sometimes this executes successfully other times it fails with the above errors.

like image 519
JSweete Avatar asked Oct 13 '12 20:10

JSweete


2 Answers

Your server is not in a passive mode, and add this code to process:

ftp_pasv($ftp, true);

For more information look at passive mode on php.net: http://php.net/manual/en/function.ftp-pasv.php Passive mode uses the data initiated by the client rather than the server. So this is why you can't put on server. If this is not set it will fail.

NOTE: Set ftp_pasv() function after ftp_login() function.

like image 51
Marin Sagovac Avatar answered Sep 28 '22 07:09

Marin Sagovac


After using ftp_pasv () the problem was still occurring. I found out that the number of requests to the server caused a problem with the firewall (I used a foreach() loop to scroll through multiple files). Because I did not have permission to modify the firewall rules, I added a sleep () in my script between the requests.

This is how i fixed the warning.

like image 22
M.V. Baks Avatar answered Sep 28 '22 06:09

M.V. Baks