how to download a file without display download request
Nov 26, 2009 Home -> Beginners, Php
Usually when we try to download something from a server we get a confirmation request whether you want to save the file or open it.
To avoid this request you can use the below code and then user can directly download that file without any message.
First give a link or a button for user to download and when they click redirect them to a file which will have the code below.
suppose you want user to download a pdf file then first make a link like this:
<a href=”javascript:void();” onClick=”location.href=’doc/document_download.php’”>download pdf</a>
Then make a file document_download.php with the code below:
<?php
// We’ll be outputting a PDF
header(’Content-type: application/pdf’);
// It will be called downloaded.pdf
header(’Content-Disposition: attachment; filename=my.pdf’);
// The PDF source is in original.pdf
readfile(’my.pdf’);
?>
Replace my.pdf with the name of your pdf.If you don’t want to hardcode the file name then you can send file name with the url like this:
<ahref=”javascript:void();” onClick=”location.href=’doc/document_download.php?fileName=my.pdf’”>download pdf</a>
and then:
<?php
// We’ll be outputting a PDF
header(’Content-type: application/pdf’);
// It will be called downloaded.pdf
header(’Content-Disposition: attachment; filename=”‘.$_REQUEST['fileName'].’”‘);
// The PDF source is in original.pdf
readfile($_REQUEST['fileName']);
?>
Related posts:
Tags: attachment, Content type, Content-Disposition, direct download, download, file, filename, pdf download, Php

Leave a Reply