Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 487633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:37:31+00:00 2026-05-13T01:37:31+00:00

I am working on a website where the visitor should be able to download

  • 0

I am working on a website where the visitor should be able to download a pdf file.
(There are three links to choose from but that is irrelevant)
I wanted to know how to make it so that the visitor can simply click the link and not have to

right click > Save (target) As...

I am open to PHP and or Javascript solutions. Thanks.

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

EDIT2: I used Nirmal’s solution in the end, since it was the simplest to change for all three files. I didn’t need to make 3 files for the three PDF’s and I didn’t need to hand code the switch. BalusC gets the check though since his/her code was up first and does the trick too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:37:31+00:00Added an answer on May 13, 2026 at 1:37 am

    All you basically need to do is to set the Content-Disposition header to attachment to get a ‘Save As’ dialogue. Here’s a kickoff PHP example:

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

    You can’t and don’t want to do this with Javascript.

    Important note: due to a poor feature, in MSIE the default filename in ‘Save As’ dialogue won’t be derived from the content-disposition header, it will instead be the last part of the pathinfo in the request URL. To workaround this, append the PDF filename to the link, e.g. http://example.com/pdf/foo.pdf. You can even make use of it in PHP to read the in the pathinfo specified PDF file. Here’s a basic example of 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');
        }
    ?>
    

    This however assumes that you’ve MultiViews on so that /pdf/ will go through this PHP file, or at least a RewriteRule from /pdf/ to /pdf.php/.

    The major advantage of this approach is that you don’t need to change the code whenever you want to add a new PDF file or change the PDF file name.

    You can even make it more generic by automatically determining and setting the correct content type:

    <?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');
        }
    ?>
    

    Name it files.php or so and then you have a generic PHP downloader which you can access by for example http://example.com/files/foo.pdf, http://example.com/files/bar.zip, etcetera.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey i guys i want help from you. i am working on website project,
Working on a website that has Employee and Branch entities, using a database table
My website is working fine on IE, but when I open it on Firefox,
I'm working on a website (tobacco related) that requires all visitors to validate they
I enjoy working with Google Analytics and the ways that I am able to
OK, this is working but I feel there is a better way to do
I'm trying to add a payment method to working website that is using a
I am working on a website that posts events throughout the year. We offer
My website relies completely on a random page generator that loads a page from
I have a working website that i am translating over to a WordPress theme.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.