How to prompt user to download a file in browser from custom module php code?
I tried first the php way
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
It silently fails. Then I realized there is a "drupal way" to do it. So I tried
drupal_add_http_header('Content-Type', 'application/octet-stream');
drupal_add_http_header('Content-disposition', 'attachment; filename='.$fname);
drupal_add_http_header('Content-Length', filesize($fpath));
but still not working. I think one possible reason for failure is that I am not sure at each step which is the correct path I should use.
Should it be '/home/mysite/public_html/sites/default/files/pdfs/'.$filename.'.pdf'
or just the part that starts in the drupal root '/sites/default/files/pdfs/'.$filename.'.pdf'
or even just 'public://pdfs/'.$filename.'.pdf'
as I see it in some other threads. I wonder why it has to be so complicated.