tags:

views:

25

answers:

1

I have a FTP with some file which I want give it to the users for Downloading. But the FTP is password protected and I want to authenticate the FTP using PHP and make the links available in a php page so as that when the User clicks on any of the FTP links in that page, the download should start.

Please help.

+4  A: 

You could use PHP's fopen wrappers to open the FTP location by PHP from your server, and pass it on to the user as a normal HTTP response:

// you can use application/octet-stream to force download
header ('Content-Type: ' . $content_type_of_your_file); 

// open file and pass it to output
readfile ('ftp://username:[email protected]/path/to/your.file');

As @Alan mentioned, if the FTP server is somewhere outside your network, this will generate additional traffic. You could wrap this in a caching scheme - e.g. only download if you don't have a copy on your web server, and if you do, check for freshness. Then you're essentially building a HTTP mirror of the FTP site, and it may be prudent to pre-fetch the files to your cache before they're requested by the users.

Piskvor
This is a great method security-wise, but bear in mind you may have problems with bandwidth limits/costs, should your webserver have them.
Alan
Usual disclaimers apply: if FTP server is unreachable, this code may exhibit similar broken behavior (waiting for the connection to time out).
Piskvor
@Alan: The security isn't that great really (FTP only supports plaintext auth). I was assuming that the FTP server sits somewhere nearby (e.g. on the same company's network where only trusted users could upload, and download was possible thru webapp). Caching and other complexity may be added at will :)
Piskvor
I was actually referring to the client-side security — the user will not see the credentials for the server, nor the fact that FTP is involved at all, which also means that anybody unable to use FTP can still download from downloads generated in this manner.
Alan