php - file download box
I'm giving a user a link to dow开发者_运维知识库nload a csv file ...just using
Click <a href="report.csv">here</a> to download your file.
This text and link is being displayed in a small popup window - height 100 width 400.
If the user clicks "save" - then no problem then the file is saved to wherever they choose
If though they choose "open" the csv file is then displayed in the small popup window. Which isn;t what I want - I'd prefer excel to open and display the file in excel, or even disabling the "open" button may be a possible option.
Any ideas how I can achieve either?
Thanks,
I assume that you are opening that popup box using some javascript .. so I'd suggest to dont show/open your tiny "download" popup box and instead show this link on a webpage. So when somebody clicks on the link force the browser to download the file and if they wish to open it they can and probably get a view which is as wide as of browswer's windows size.
// just for example, to force download of a zip file, send the headers as
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
You have no power over users' settings such as which file type is opened by which application. They would have to go to their settings and change the file association. However, bear in mind not everyone has Excel installed, so it's not a good idea to force the user to open the csv file in this particular application.
I also don't think you can disable the Open option, this is also dictated by the web browser's configuration whether it will or will not display the window or will it launch the associated application directly etc... it seems you're out of luck in your particular case.
I believe this is OS-level and can't be changed.
One half-solution is adding target="_blank"
to the tag. That way, when users select the 'open' option, at least the file will be opened in a new (full-screen, or at least resizable) window.
Other than that, you can't change the behaviour of the window, as ceejayoz stated.
- Make sure you don't do any other output before this point
- Build your csv string and store it in $csvString
- Give it the appropriate filename in $csv_filename
The headers should tell the browser to load it in excel
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=$csv_filename");
echo $csvString;
精彩评论