How to create a link to download a PDF file that does not require a right-click?

I work on a website where a visitor should be able to download a pdf file. (There are three links to choose from, but that doesn’t matter) I wanted to know how to make the visitor just click the link and not have

right click > Save (target) As...

I am open to PHP and Javascript solutions. Thanks.

EDIT: Can I use javascript to call PHP and save the file via AJAX?

EDIT2: I used the Nirmal solution at the end as it was the easiest for all three files. I did not need to make 3 files for three PDFs, and I did not need to transfer the code to the switch. BalusC receives a check since his / her code was the first and does the trick.

+3
source share
6 answers

All you have to do is set the Content-Dispositiontitle attachmentto get the Save As dialog. Here is an example PHP for beginners:

<?php
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment;filename="foo.pdf"');
    readfile('/path/to/foo.pdf');
?>

You cannot and do not want to do this with Javascript.

Important Note: Due to a poor function in MSIE, the default file name in the Save As dialog box will not be displayed from the header Content-Disposition, it will instead be the last part of the path in the request URL. To get around this, add the PDF file name to the link, for example. http://example.com/pdf/foo.pdf. You can even use it in PHP to read in a specified PDF file with a path. Here is a basic example pdf.php:

<?php
    $file_name = $_SERVER['PATH_INFO'];
    $file = '/path/to/pdf/files' . $file_name;
    if (file_exists($file)) {
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
        header('Content-Length: ' . filesize($file));
        readfile($file);
    } else {
        header('HTTP/1.1 404 Not Found');
    }
?>

, MultiViews, /pdf/ PHP , , RewriteRule /pdf/ /pdf.php/.

, , PDF PDF.

, :

<?php
    $file_name = $_SERVER['PATH_INFO'];
    $file = '/path/to/all/files' . $file_name;
    if (file_exists($file)) {
        header('Content-Type: ' . mime_content_type($file_name));
        header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
        header('Content-Length: ' . filesize($file));
        readfile($file);
    } else {
        header('HTTP/1.1 404 Not Found');
    }
?>

files.php , PHP-, , , http://example.com/files/foo.pdf, http://example.com/files/bar.zip ..

, .

+12

, PHP-, Apache, .htaccess , PDF:

<Files *.pdf>
  Header set Content-Disposition attachment
</Files>

-, IE/Adobe Reader Content-Disposition. ForceType application/octet-stream

<Files *.pdf>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>
+3

, , HTML5 download.

:

<a href="myfile.pdf" download>Download</a>

. w3schools :

Chrome (14+), Firefox (20+), Opera (15 +).

, attr:

<a href="myfile.pdf" download="NewName.pdf">Download</a>
+2

PHP , Content-type: application/octet-stream.

+1

HTTP- , PHP Docs (. )

+1

:

<?php
if(isset($_GET['docid'])){
    switch($_GET['docid']){
        case '1':
            $file = 'complete/path/to/pdf/file1';
            break;
        case '2':
            $file = 'complete/path/to/pdf/file2';
            break;
        case '3':
            $file = 'complete/path/to/pdf/file3';
            break;
        default:
            exit;
    }

    if(file_exists($file)){
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

php (, download.php) . script, , pdf . .

PDF, href '/path/to/download.php?docid=1'
pdf, href '/path/to/download.php?docid=2'
PDF, href '/path/to/download.php?docid=3'

, AJAX .

+1

Source: https://habr.com/ru/post/1725241/


All Articles