开发者

PHP - List of Excel files to download from Intranet Site?

开发者 https://www.devze.com 2022-12-11 19:47 出处:网络
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

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开发者_JS百科['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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号