3

I try to download a file from FTP server into client. If I use ftp_get, the file is downloaded into PHP server, which can write the output into browser. So the download process is

FTP server -> PHP server -> client

This doubles traffic - this is bad in downloading big files. There is a way how to write the file directly into the browser described here: Stream FTP download to output - but the data flows through PHP server anyway, am I right?

Is there any way how to establish this download (if yes, how?), or is it principially impossible?

FTP server -> client

Edit: it should work also with non-anonymous FTP servers in secure way.

Community
  • 1
  • 1
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • No, this won't work with password protected servers. The user will have to directly connect to the FTP server, for which he'll need to know the password. – deceze Apr 30 '11 at 00:22

3 Answers3

3

<a href="ftp://server/file.ext">Download the file</a> ;-)

Capsule
  • 6,118
  • 1
  • 20
  • 27
2

If the client can directly access the file in question (i.e. no secret usernames or passwords necessary), just redirect him to it:

header('Location: ftp://example.com/foobar');

This will cause the client to access the URL directly. You can't control what the client will do though. The browser may simply start to download the file, but it may also launch an FTP client or do other things which you may or may not care about.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

try below code for that.

$curl = curl_init();
$file = fopen("ls-lR.gz", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);

Thanks.

Chandresh M
  • 3,808
  • 1
  • 24
  • 48
  • This seems to me to be the best solution, I will make some tests. You could point the original source I have found: http://stackoverflow.com/questions/1178425/download-a-file-from-ftp-using-curl-and-php – Jan Turoň Apr 29 '11 at 11:39