1

I have a download page where i have kept musics, pictures, ebooks and etc. Whenever users click at the image it opens it, and same things happen with .pdf files if the user has installed pdf reader on their PC?

I dont want them to open the file but to download it? Any ideas

Salman A
  • 262,204
  • 82
  • 430
  • 521
Starx
  • 77,474
  • 47
  • 185
  • 261
  • 1
    Multiple duplicates. Search "PHP force download" – Pekka May 13 '10 at 10:27
  • possible duplicate of [Forcing to download a file using PHP](http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php) – Pekka May 13 '10 at 10:28

3 Answers3

5
header('Content-disposition: attachment; filename=NAME_OF_YOUR_FILE');
Salman A
  • 262,204
  • 82
  • 430
  • 521
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

You have to add a header to force the download.

header("Content-Disposition: Attachment")
header("Content-Disposition: Attachment;filename=FILENAME_HERE")

from http://www.symkat.com/force-download-with-http-headers

Elph
  • 1,594
  • 1
  • 15
  • 20
0

With Apache mod_headers:

SetEnvIf Request_URI "([^/]+\.jpg)$" REQUESTED_FILE_BASENAME=$1
SetEnvIf Request_URI "([^/]+\.mp3)$" REQUESTED_FILE_BASENAME=$1
SetEnvIf Request_URI "([^/]+\.pdf)$" REQUESTED_FILE_BASENAME=$1
Header set Content-Disposition "attachment; filename=\"%{REQUESTED_FILE_BASENAME}e\"" env=REQUESTED_FILE_BASENAME

The instructions can be placed inside a .htaccess file which itself must go in the directory containing your media. You use just about any extension. The following line might also help against aggressive plugins:

Header set Content-Type "application/octet-stream" env=REQUESTED_FILE_BASENAME
Salman A
  • 262,204
  • 82
  • 430
  • 521