Jump to content

[SOLVED] PHP Warning: ftp_fget(): Can't open data connection


brianstorm

Recommended Posts

Hi,

 

I have just moved a website to a new server. I had a working script (Apache 1.x, RH6.x) which connected to an ftp site and downloaded a csv file (ifd it had changed).... this script now doesn't work on the new server (Apache 2, Centos 4)... The only warnings I have been able to find are in the httpd error log and state the following

 

PHP Warning:  ftp_fget(): Can't open data connection

 

I am not sure where to look now to fix the problem... I have tried a new basic script from the php manual (same problem), I have tried various differnet chmod and chown permutations. The script itself runs and does create a new file to write to, but always the can't open data connection problem. (The ftp connection is good when used with an ftp program).

 

I am not a firewall expert but my iptables script looks to be allowing all outgoing connections, all established and related connections, and all new port 21 incoming connections whcih as far as i know covers this.... anyway, here's the script.... if anyone can give me any ideas I'd be most grateful...

 

running this shows the "Could not download file" error (if i delete empty file), passwords, paths & ips changed just in case

 

 

<html>

<head>

  <title>Mirror update</title>

</head>

<body>

<h1>Mirror update</h1>

<?php

 

//echo  phpinfo();

// set up variables - change these to suit application

$host = 'ip address here;

 

$user = 'xxxxxxx';

$password = 'xxxxxxxxxx;

 

$remotefile = 'myfile.csv';

$localfile = '/var/www/html/www.pathtofolder/www/subfolder/myfile.csv';

 

 

// connect to host

$conn = ftp_connect("$host");

if (!$conn)

{

  echo 'Error: Could not connect to ftp server<br />';

  exit;

}

echo "Connected to $host.<br />";

 

// log in to host

@ $result = ftp_login($conn, $user, $password);

if (!$result)

{

  echo "Error: Could not log on as $user<br />";

  ftp_quit($conn);

  exit;

}

echo "Logged in as $user<br />";

 

// check file times to see if an update is required

echo 'Checking file time...<br />';

if (file_exists($localfile))

{

  $localtime = filemtime($localfile);

  echo 'Local file last updated ';

  echo date('G:i j-M-Y', $localtime);

  echo '<br />';

}

else

  $localtime=0;

$remotetime = ftp_mdtm($conn, $remotefile);

if (!($remotetime >= 0))

{

  // This doesn't mean the file's not there, server may not support mod time

  echo 'Can\'t access remote file time.<br />'; 

  $remotetime=$localtime+1;  // make sure of an update 

}

else

{

  echo 'Remote file last updated ';

  echo date('G:i j-M-Y', $remotetime);

  echo '<br />';

}

if (!($remotetime > $localtime))

{

  echo 'Local copy is up to date.<br />';

  exit;

}

 

// download file

echo 'Getting file from server...<br />';

$fp = fopen ($localfile, 'w');

if (!$success = ftp_fget($conn, $fp, $remotefile, FTP_BINARY, 0))

{

  echo 'Error: Could not download file';

  ftp_quit($conn);

  exit;

}

fclose($fp);

echo 'File downloaded successfully';

// close connection to host

ftp_quit($conn);

 

 

 

?>

</body>

</html>

i found a solution myself after a bit of reading i discoverd that active ftp connections also use port 20 so the firewall was blocking the connection...

 

inserting this into my script fixed the problem

 

//////////TURN ON PASSIVE MODE

ftp_pasv($conn, true);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.