PHP’s FTP isn’t working on Amazon linux EC2 instance

amazon ec2amazon-web-servicesftpPHP

I set up a m1.large EC2 instance and want to use it to download logs off a cdn's server. I had the code working just fine on my ubuntu laptop but once uploaded to the server the ftp_rawlist() function stopped working. As far as i can tell the code is still logging into the ftp server but rawlist isn't returning any files.

here is the sample code:

<?php

$FTP_USERNAME = 'username';
$FTP_PASSWORD = 'pass';
$FTP_SERVER   = 'cdn.server';
$FTP_LOGDIR   = 'logs';

$conn_id = ftp_connect($FTP_SERVER) or die('Couldn\'t connect to '.$FTP_SERVER);

if(!ftp_login($conn_id, $FTP_USERNAME, $FTP_PASSWORD)) {
        die('Couldn\'t connect as '.$FTP_USERNAME);
}

ftp_chdir($conn_id, $FTP_LOGDIR);

$rawlist = ftp_rawlist($conn_id, '.');

ftp_close($conn_id);

var_dump($rawlist);
?>

and the output is this:

bool(false)

even though i know the directory isn't empty.

When setting up the server these are the commands i used to install everything:

sudo yum install php
sudo yum install php-mysql
sudo yum install php-pear
sudo pear channel-discover pear.amazonwebservices.com
sudo pear install aws/sdk

I even opened all the ports for the security group but even then the output was still bool(false). And php -i shows that ftp is enabled.

Any help would be greatly appreciated, i have been racking my brain all day trying to figure this out.

Best Answer

Needed to run it in passive mode

ftp_pasv($conn_id, true);