tags:

views:

47

answers:

2

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files.

Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered into a list of hyperlinks. These hyperlinks could then be selected to download the excel file.

What I have so far is:

//get search parameters (from a form)
$s_Date = $_GET['s_Date'];
$s_Shift = $_GET['s_Shift'];
$s_Name = $_GET['s_Name'];

//search folder
foreach(glob("c:\folderA\folderB\*".$s_Date."*".$s_Shift."*".$s_Name."*.*") as     $FileName)
{
    echo basename($FileName);
    echo"<a href=?myad=".basename($FileName)."/>Download</a>"."<br />";
}

This returns list of files but selecting hyperlinks doesn't prompt for download.

How do I get the hyperlink to force a content-type of msexcel?

+5  A: 

Assuming you are using a PHP script to deliver the file conent, the script needs to set the Content-Type header:

<?
    header('Content-Type: application/excel');
    header('Content-Disposition: attachment;filename=myfile.xls');
    // send file content
?>

Note you can also set the name to use for the file using the Content-Disposition header.

You might also want to consider a more well-formed <a> tag that points to your download script:

echo '<a href="dl.php?myad=' .urlencode(basename($FileName)). '">Download</a><br />';

In this example, you need to replace dl.php with your actual script.

jheddings
+1  A: 

Also append Content-Length header:

header('Content-Length : ' . filesize('myfile.xls'));

Client would like to know how big file is and how much of it has already been downloaded.

Peter