Brett Powell Brett Powell - 1 year ago 132
PHP Question

Download FTP file to client w/ Readfile()

I'm building a web based file management interface for our clients, where I am attempting to facilitate a download from a remote (FTP) server to the client without downloading to the local web server first.

I've found that

does exactly what is needed, working perfectly for both web based downloads as well as from public FTP servers. The problem is that when specifying credentials via the FTP url, it apparently no longer works. I've found other reports of this online but thus far no solutions or workarounds.

$file_url = 'ftp://username:password@';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");

Is there any workaround methods that would make this operate as expected? I am stumped as to how this can be fixed, where it seems like a bug moreso than a limitation.

Answer Source

The URL-encoded spaces (%20) in the URL seems to the culprit.

Just use literal spaces:

$file_url = 'ftp://username:password@ port 25665/';

It's perfectly valid to specify the credentials this way in PHP FTP URL wrapper.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download