Sam
Sam

Reputation: 840

Providing Links for Download to Password Protected FTP

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.

Upvotes: 1

Views: 953

Answers (1)

Piskvor left the building
Piskvor left the building

Reputation: 92752

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.

Upvotes: 5

Related Questions