Page 1 of 1

Download file - prompt for file name

Posted: Sat Sep 12, 2009 9:33 am
by flycast
I am writing code that ends with a file download. My code creates the file name so the user cannot rename the file until after it is downloaded and saved to their hard drive. I want a suggested name to appear in the save dialogue but I want the user to have the option to change the name if they want. I have looked and found many examples of the version I am using that give the user no control over the name. Here is my code:

Code: Select all

               $filename = $data_store['exp_weblogs']['data'][0]['blog_title']
                    .date(" m-d-Y g.i.s a")
                    .'.wblg';
                $browser_agent = "";
                $mime_type = ($browser_agent == 'IE' || $browser_agent == 'OPERA') ? 'application/octetstream' : 'application/octet-stream';
                header('Content-Type: ' . $mime_type);
                if ($browser_agent == 'IE')
                {
                   header('Content-Disposition: inline; filename="' . $filename . '"');
                   header("Content-Transfer-Encoding: binary");
                   header('Expires: 0');
                   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                   header('Pragma: public');
                } else {
                   header('Content-Disposition: attachment; filename="' . $filename . '"');
                   header('Content-Disposition: attachment; filename=""');
                   header("Content-Transfer-Encoding: binary");
                   header('Expires: 0');
                   header('Pragma: no-cache');
                }
                exit( serialize($data_store));

Re: Download file - prompt for file name

Posted: Sat Sep 12, 2009 9:45 am
by Eran
You are overwriting your filename header with your second line on the else condition.

The following should work cross browser:

Code: Select all

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); 
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".$size);
header('Content-disposition: attachment; filename="' . $filename . '"');

Re: Download file - prompt for file name

Posted: Sat Sep 12, 2009 4:07 pm
by flycast
Yes. I was. I have discovered that this is a Firefox browser setting. In Preferences "Downloads" there is an option "Always ask me where to save files." Thanks for your help.