form vs href链接损坏

So I have been trying to get upload/download system to work but kept running into problems, specifically, the downloaded file being corrupt (.xls).

This is what I have currently:

<form action="{{block type=" core/template" name="my-template"
template="php/download_file.php" }}" method="post">
<label for="date">Date:</label>
<input type="date" name="date" id="date"><br>
<input type="submit" name="submit" value="Download">
</form>
<a href="link/to/file">download file</a>

So, these both link to the same file and downloads fine. If I click on the download file link, file opens perfectly fine. If I go through the form download button, then it'll download but opening gives me a warning/error: "the file format and extension of 'file' don't match" and just hangs forcing me to force close the file.

download_file.php:

<?php
if($_POST['submit']) {

$file = 'excel_file.xls';
// Magento file path
$path = Mage::getBaseUrl('media') . 'folder' . DS . $file;

header("Content-Type: application/vnd.ms-excel");
//header("Content-Type: application/octet-stream");
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename = 'filename'");
echo $path;
exit;
}
    ?>

This is all in a Magento Static Block, in case that has anything to do with it. Any help is appreciated. Thanks.

Have you tried this:

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");  //File name extension was  wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

Greetings.

Try changing:

header("Content-Disposition: attachment; filename = 'filename'");

to:

header("Content-Disposition: attachment; filename=filename.xls");

Or if you use a variable:

header("Content-Disposition: attachment; filename=$filename.xls");

Basically, start and end with a quote and don't use any quotes or apostrophes in between.