Download file - prompt for file name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Download file - prompt for file name

Post 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));
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Download file - prompt for file name

Post 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 . '"');
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Re: Download file - prompt for file name

Post 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.
Post Reply