Re: [PHP-DB] force to download file

This is a discussion on Re: [PHP-DB] force to download file within the PHP General forums, part of the PHP Programming Forums category; On Dec 17, 2007 3:13 PM, Hiep Nguyen <hnguyen@jadesterling.com> wrote: > hi all, > > ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-17-2007
Daniel Brown
 
Posts: n/a
Default Re: [PHP-DB] force to download file

On Dec 17, 2007 3:13 PM, Hiep Nguyen <hnguyen@jadesterling.com> wrote:
> hi all,
>
> i have this on top of my php page:
>
> header("Content-Type: application/vnd.ms-excel");
> header("Content-Disposition: inline; filename=excelfile.xls");
>
> but it is not prompt to save the file instead it opens right in IE.
>
> my question is how do i force the browser prompts to save the file?
>
> thanks


Hiep,

This is a question that should've been asked on the PHP General
list, so I'm reply-all'ing and sending it to the General list for the
archives as well.

Here's a function I use that should help you out.

<?
function force_download($filename,$dir='./') {
if ((isset($file))&&(file_exists($dir.$file))) {
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="'.$dir.$filename.'"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($dir.$filename));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($dir.$filename);
} else {
echo "No file selected";
}
}
?>


--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
Reply With Quote
  #2 (permalink)  
Old 12-18-2007
Richard Heyes
 
Posts: n/a
Default Re: [PHP] Re: [PHP-DB] force to download file

>> i have this on top of my php page:
>>
>> header("Content-Type: application/vnd.ms-excel");
>> header("Content-Disposition: inline; filename=excelfile.xls");
>>
>> but it is not prompt to save the file instead it opens right in IE.
>>
>> my question is how do i force the browser prompts to save the file?


<?php
header('Content-Disposition: attachment; filename="'.$filename.'"');
?>

That should do the trick, but if not then try adding the Content-Type
header from below.

> <?
> function force_download($filename,$dir='./') {
> if ((isset($file))&&(file_exists($dir.$file))) {
> header("Content-type: application/force-download");
> header('Content-Disposition: inline; filename="'.$dir.$filename.'"');
> header("Content-Transfer-Encoding: Binary");
> header("Content-length: ".filesize($dir.$filename));
> header('Content-Type: application/octet-stream');
> header('Content-Disposition: attachment; filename="'.$filename.'"');
> readfile($dir.$filename);
> } else {
> echo "No file selected";
> }
> }
> ?>


FYI You have Content-Disposition twice; you only need the second.

--
Richard Heyes
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

** NOW OFFERING FREE ACCOUNTS TO CHARITIES AND NON-PROFITS **
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0