views:

330

answers:

2

Hi,

I am trying to download a file with PHP from an FTP server which i can access from http too. For example: FTP://username:[email protected]/file_name_here.gz. The file is forced as an attachment from the server (not as a PDF or TXT would do eg: output). I tried it with 3 different methods:

  1. file_get_contents -> Return a zero size file
  2. Curl -> times out
  3. FTP function -> Returns a zero size file

I think CURL is the way to do it but I can't manage to download the file... Here is my code:

$curl = curl_init();
$url ='ftp://'.$network['username'].':'.$network['password'].'@'.$network['metadata_url'].'/'.$path.'/'.$data['title'];
curl_setopt($curl, CURLOPT_URL,$url);
//curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$file = fopen("./feeds/".$data['title'], "w");
curl_setopt($curl, CURLOPT_FILE, $file);
$result = curl_exec ($curl);

I tried both setting the username and password in the CURLOPT_URL and setting them with CURL. The same thing happens with both of the ways...

Can you help?

Edit: Forgot to mention that the file is served from the server dynamically. So for example the real file is: filename.gz.check which when i ommit the .check part i get the original file. Dunno why they do this but I need to work with this as it is.

+1  A: 

(One note: the notion of "forced as an attachment" doesn't make sense in the context of an FTP server. FTP servers just serve bits; it's up to your FTP client to decide what to do with them. This is in contrast to HTTP servers, which serve those bits but also serve metadata about the bits (e.g., the HTTP headers); it's that metadata that instructs your client what to do (display vs. force a download/save action). When you use your browser as an FTP client, the browser makes a decision about how to handle the response to an FTP GET request, and that process is entirely separate from how it makes a decision about how to handle the response to an HTTP GET request.)

Have you tried your code with hardcoded values first, to make sure that the issue isn't that you're passing in unexpected or unanticipated value data in the $network and $_FTP variables? In other words, test this, substituting a known-good username, password, hostname, and filename, and making sure you have rights to write a file to the current working directory:

$curl = curl_init();
$url = "ftp://hostname/filename.gz";
$file = fopen("filename.gz", "w");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_exec($curl);
curl_close($curl);
fclose($file);

In any event, you can also add the following to get debugging information sent back to your browser:

curl_setopt($curl, CURLOPT_VERBOSE, 1);

If you add that to your code, what debugging information do you get back?

delfuego
I get nothing...I know that the values used are ok (I checked them with echo to see the url and parameters passed to curl)
jeezTech
A: 

For the cURL based solution try without any timeout by:

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,0);

You can also try this program and let us know what happens:

<?php

$conn = ftp_connect("ftp.someserver.com") or die("Could not connect");

ftp_login($conn,"username","password");

echo ftp_size($conn,"source.tgz");

echo ftp_get($conn,"target.tgz","source.tgz",FTP_BINARY);

ftp_close($conn);

?> 
codaddict
No luck with FTP. Tried this already...
jeezTech