Prompting a user to save the data we are sending

In many projects we create reports and we want the data to be exported in csv file so that the user can download it and send it via email to some other department or we need to provide links to some files and want to force the user to download these file. We can use the Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.


<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

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

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Here we need to make sure that no output is sent to the user browser before using the header() function even blank lines.

Leave a Comment

Back to top