FTP PHP Connect, Open a File and Display Information


I’m trying to achieve the following with PHP.

I want to FTP connect to a server, open a TXT file and then display this data on my web site.

The data will be something like this:

Property One | 32,000 | Garden
Property Two | 42,000 | Garden & Hose

Where do I begin?

Here’s where I am…


$ftp_server = "ftp.example.com";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");


I would love help with this.

Thank you

The manual has pretty good example explaining how to obtain a file.


Alternatively, you can use the standard file_get_contents etc using the inbuilt FTP stream wrapper.


Once you have it, concentrate on caching/manipulation.


$data = file_get_contents('ftp://mirror.aarnet.edu.au/pub/ubuntu/releases/robots.txt');
  Remote Data
<pre><?php echo $data; ?></pre>

I’m going to trial this shortly and see how I get on.

Thanks again :slight_smile:

Hi Anthony,

Understand you’re probably crazy busy this morning.

I’ve tried the script you made above, but the script seems to keep timing out.

I changed

$data = file_get_contents('ftp://mysite.com/robots.txt');


$data = file_get_contents('http://mysite.com/robots.txt');

and it worked fine.

Would this suggest then that my server setup is blocking FTP access via a PHP script?

May speak with the host to get their thoughts.

Why, yes. Yes I am. :stuck_out_tongue:

However, not too busy for a spot of SitePoint.

Does the FTP connection require credentials? If so, you need to specifiy them in the URI: [noparse]ftp://user:password@example.com/pub/file.txt[/noparse]

Also, if you have http access, why are you using FTP? I assume you’re only reading the data?


Aaah. Good call.
It’s just the chappy I’m dealing with specified FTP read only access, so I’m assuming HTTP is not “where it’s at”. Will double checkify.

Thanks again, I will try the credentials and see how it rolls.