0

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

Juan Ignacio
  • 3,217
  • 8
  • 33
  • 53
  • Are the URLs that you generate pointing back directly to the NAS? If so, you could have your web application serve as a middle man between the NAS and the browser, in which case you will be able to add whatever header you may please. – Tarik Jul 08 '13 at 07:18

1 Answers1

0

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.

user2557188
  • 150
  • 1
  • 8
  • I thought of that but I can only run a php file for 2 minutes, and if the user is downloading a video it will most probably take more than that and it will get cut off when it times out. Still, I think this is the only option – Juan Ignacio Jul 08 '13 at 19:00