How can I force a file download using php?

download, php, readfile

Solution

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);

Problem

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!

Original source