I am trying to write a PHP script that will download a file on the client side. The file can be Excel (xlsx) or JSON.
The URL will look like:
http://<server>/php/download.php?file=/some/path/file.xlsx&name=file.xlsx&format=xlsx
And my PHP script looks like this:
<?php
if($_GET['format']==='excel') {
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else if($_GET['format']==='json') {
header('Content-type: application/json');
}
header("Content-disposition: attachment; filename=".$_GET['name']);
readfile($_GET['file']);
?>
I have double checked that the actual files are not corrupt or anything. But when I use the URL above, a 0 sized file is downloaded (albeit with the right name) and obviously that's wrong. What is wrong in my PHP code?