Jump to content

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


DeX

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

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).

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.