Download file from FTP server directly to browser

2020-04-23 02:38发布

问题:

ftp_get() downloads a file from an FTP server and saves the file to local server.

So when I want to download a file from an FTP server to my browser, the file will first be downloaded to the local server and then downloaded to the browser.

This causes double bandwidth. Is there a way to download a file from an FTP to browser directly?

回答1:

ftp_get() or curl or any PHP script will require opening a stream to the source, and passing it the client browser. You still use 2 streams, resulting in double the bandwidth usage. The only way to avoid this is to link to or have the end-user collect the file directly.

I am assuming that you're collecting the file from a private FTP location, passing the credentials, and you do not want the end-user to have these or they do not know them. Yet for them, it should be a seamless download.

Not a lot of good ways to do this. In my mind, making an FTP Client connection via Flash in the end-users browser is one way. You could dynamically create flash or have the flash collect the credentials (encrypted), and then perform the connection to the FTP Server from the end-users browser (after decrypting the credentials) and download the file directly to the end-user.



回答2:

All you can do is to redirect the client browser to the ftp:// URL. That's doable when the FTP site allows an anonymous read access. Most (all) web browsers support FTP natively.

Depending on a workflow, you either redirect from the PHP code:

header("Location: ftp://download.example.com/file.pdf");

On you directly use ftp:// URL in the HTML code:

<a href="ftp://download.example.com/file.pdf">Download</a>

If anonymous read access is not allowed, you'd have to include the credentials in the URL, what you probably do not want to.

ftp://username:password@download.example.com/file.pdf


标签: php ftp