What you are trying to do is not practical to check this question; Header Location + Content Placement
Content-Disposition + Title Header
But you can do this to make it work, you will need to postpone the whole answer until it is sent. You can do this with output buffering
Otherwise, the browser may interpret the Location header before downloading the file. This is sketchy anyway, so you should not do this.
Note that forcing "as" using Content-Disposition: attachment; ensures that the client will not move anywhere, so the method below should be in any case.
Stream file in php
To simply point out a guy who has brains in the right place :
// To use header() with 'content-type', why don't you use mime_content_type() function rather than checking the type on the basis of extension? // Example code: <?php $file="test.docx"; header("Pragma: public"); header('Content-disposition: attachment; filename='.$file); header("Content-type: ".mime_content_type($file)); header('Content-Encoding: identity'); ob_clean(); flush(); readfile($file); ?> // Use $file to map to whichever type of file. // Note: the mime types should already be defined in apache settings
Source: http://www.php.net/manual/en/function.header.php#107581
Note that the original response uses Content-Transfer-Encoding , which does not actually exist in HTTP. The comment below this source explains this: http://www.php.net/manual/en/function.header.php#107044
source share