Jump to content

How can users save a PDF which I'm loading with readfile()?


DeX
Go to solution Solved by requinix,

Recommended Posts

I don't want my users to be able to load all PDF on my system so I verify them with a token and then load their PDF like so:

if ($location)
    {
        header('Content-Type: application/pdf');
        readfile($location);
    }

The problem is when they right click it to save, it tries to save as pdf-loader.php instead of the quote-123.pdf filename. They're trying to save them as the quote numbers so they are easily matched up with each customer quote and they're not the most savvy computer users. Even when using the Chrome menus to select SAVE, it still tries to save the same way.

Link to comment
Share on other sites

  • Solution

If you don't mind forcing a download instead of presenting it in the browser then you can do

header('Content-Disposition: attachment; filename="quote-123.pdf"');
Otherwise make the URL to the PDF be something that ends in /quote-123.pdf.

You can use URL rewriting (nicer) or change your /path/to/script.php to be like /path/to/script.php/quote-123.pdf which should still invoke your PHP script (easier).

  • Like 1
Link to comment
Share on other sites

If you don't mind forcing a download instead of presenting it in the browser

If you use inline instead of attachment it'll still display in the browser while also using the custom filename (in chrome and firefox at least).

Edited by kicken
  • Like 2
Link to comment
Share on other sites

Thank you very much! I had to make a slight change, this is what ended up working.


        // set application type as PDF
        header('Content-Type: application/pdf');
        // set file to display on screen with downloadable filename
        header('Content-Disposition: inline; filename="' . $filename . '"');
        readfile($location);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.