I have pdf files on my site for download. And when I click on the link, the browser opens the file in a separate tab. I need to get the window open - Open - Save ... or this file was downloaded immediately. Help with code
3 answers
The forum already had a similar question , look. It is possible to realize what you need through .htaccess
<FilesMatch "\.(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
|
Your question is also similar with this . The solution may be as follows:
1 There is a php-page with the following code:
$file_name = "files/file.pdf"; header("Content-disposition: attachment; filename=$file_name"); header("Content-type: application/pdf"); readfile("{$_SERVER["DOCUMENT_ROOT"]}/$file_name");
2 In the place where you had a link to the pdf of the form href="files/file.pdf"
now there should be a link of the form href="download.php"
.
PS Perhaps it would be better to use " header("Content-type: application/force-download");
" as the third line.
- Yes, but this option is not always acceptable + just aesthetics, where it is more pleasant to see the link to the pdf file, and not to download.php - Pavel Azanov
- one.htaccess will save this world :) - Alex Kapustin
- oneThanks, you are right, the download.php heap option is not acceptable (or complex). The appearance of the link does not bother anyone, but it can also affect perception (negatively). But you can write a universal script of the type download.php? File = ..., in which you can install all the necessary restrictions, for example, to give only files whose names are in some database table. Although the whole question here is that it is simpler to edit .htaccess (if not all pdfs need to be forced to download) or write such a script. - ivkremer
- I think the choice is for Sean =), by the way in the case of .htaccess it is not necessary to force all files to download - you can add a condition. of course, without queries to the database, but still - Pavel Azanov
|
As an option - pack it in the archive, without unnecessary red tape.
|