Domanda

I have a PHP that scans the files in a remote NAS Hard Disk via FTP protocol, generates a json file and then via javascript I list those files in the browser.

When the user click a link to a mp4, jpg and many browser-known formats, the browser opens te content instead of downloading it.

Now, I know that with PHP or .htaccess I can change the headers to force the browser to download the file but the file is in a remote location and can only be access via FTP so I can't run PHP or .htaccess in it.

I tried this header variations in PHP:

header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"$file\""); 

or

header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$file\"");

or

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/image");
header("Content-Transfer-Encoding: binary");

all ending with:

header("Location: $url");

(url being ftp://user:password@dyndns.org/folder/file.mp4) but it always opens the file in the browser instead of downloading (on recognized file extensions of course)

Any ideas? Thanks

È stato utile?

Soluzione

The previous headers may not work when using a redirect. Instead, you better serve them via PHP instead of redirecting:

header("Content-type: octet/stream");
header("Content-disposition: attachment; filename=".$file);
header("Content-Length: ".filesize($file));
readfile($file);

But note, that this WILL use your own bandwidth. However, there isn't any way to force a behavior on a remote host.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top