All i need help in downloading a file through headers its working fine but the problem is that the file is place in a folder and i don’t understand how to give path for that specific file. the file downloads always give me empty file. My basic sanario is that i click a link(image) for download a file on page named(documents.php) which send me to the other page named download.php.in that page i set header for downloading the clicked file like
this is a link (document.php)
<a href="download.php?title=<?=$arr['title']?> "><img src="images/xl_icn.jpg" alt="" width="19" height="20" /></a>
this is header(download.php)
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header("Content-Disposition: attachment; filename='".$_GET['title']."'");
if(isset($_SESSION)){
header('Location: documents.php');
}
and after downloading it send me back to the documents.php page but the files which i want to download is in folder uploads/documents and i dont know how to download that files placed in uploads/documents.
You have a good start I would modify your code to be more like the following:
$path you would update for what folder
The if statement works the required file calls and echos the page content in a buffer style.
This can be further expanded to include a switch statement depending on extensions to change the Content-type declaration. Since you will buffer the content to the page, you cannot call
header("Location");. Or you will get “header cannot be sent since page content has loaded” due to the buffering.I would suggest opening it in a new window or a iframe (wrapped in a div and could be hidden) on the page. You could also include it in the document.php file as well instead of calling a separate file.