The basic way to work with file attachments for users to download files via hyperlinks in PHP

This is always something that seems to be overcomplicated, if you generate PDFs or other files and use some form of AJAX to call that file, how do you force the user’s browser to treat the link as an attachment for them to save on their computer? You have to make sure the script hasn’t sent any output and send the headers along with the file data in the following sequence:

// "pdf" can be any other type of valid file
header('Content-type: application/pdf');

// File will be called "document.pdf"
header('Content-Disposition: attachment; filename="document.pdf"');

// The actual PDF file on the server can be called anything.  This is a clever way to obfuscate the real file name from the public, by using a different naming convention.


About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *