Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a file download using php?

I am new to programming, just learning it at school, but i want my users download some excel files from my homepage. The following code isn't working, it only displays it in the browser and not forcing a download dialogue.

How can i solve this problem?

The link should be: http://myurl.com/download.php?fileid=1 or http://myurl.com/download.php?fileid=2 and so on.

<?php
switch ($_GET["fileid"]) {
    case 0:
        $file = "files/mon.xls";
        break;
    case 1:
        $file = "files/uru2.xls";
        break;
    case 2:
        $file = "files/oppo23.xls";
        break;
}
readfile($file);

Thanks for your help!

like image 818
noxusbra Avatar asked Apr 28 '12 14:04

noxusbra


1 Answers

You have to use header. Like you can read on the php manual, its for your file:

<?php
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename="downloaded.xls"');
readfile($file);
like image 119
Neysor Avatar answered Sep 22 '22 20:09

Neysor