By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,038 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,038 IT Pros & Developers. It's quick & easy.

Headers and "save as" dialog box.

P: n/a
I have the following code which executes successfully to call the browsers
"save as" box...

//Screensavers
if ($FileType == 'scr')
{
// We'll be outputting a screensaver
header('Content-type: application/octet-stream');
// It will be called test.scr
header('Content-Disposition: attachment; filename="test.scr"');
// The location of the file
readfile('http://'.$FileURL);
}

The information for file locations etc is pulled from a database. What I
would like to do however is to be able to pull the filename from the
database and insert it as the filename, ie:

header('Content-Disposition: attachment; filename="$Name"');

But when I try to execute this, the "save as" box asks to save the file
$Name, and if I change the line to:

header('Content-Disposition: attachment; filename=$Name');

It then prompts to save the php code that is currently running.

Is there any way to insert the actual name of the file you are trying to
"save" rather than a generic name.

I've also tried writing:

echo "header('Content-Disposition: attachment; filename=\"$Name\"');";

But I get similar results.

Thanks for any help.

Dariusz
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 31 Mar 2004 14:22:24 +0000, Dariusz wrote:
I have the following code which executes successfully to call the browsers
"save as" box...

//Screensavers
if ($FileType == 'scr')
{
// We'll be outputting a screensaver
header('Content-type: application/octet-stream');
// It will be called test.scr
header('Content-Disposition: attachment; filename="test.scr"');
// The location of the file
readfile('http://'.$FileURL);
}

The information for file locations etc is pulled from a database. What I
would like to do however is to be able to pull the filename from the
database and insert it as the filename, ie:

header('Content-Disposition: attachment; filename="$Name"');

But when I try to execute this, the "save as" box asks to save the file
$Name, and if I change the line to:

header('Content-Disposition: attachment; filename=$Name');

It then prompts to save the php code that is currently running.

Is there any way to insert the actual name of the file you are trying to
"save" rather than a generic name.

I've also tried writing:

echo "header('Content-Disposition: attachment; filename=\"$Name\"');";

But I get similar results.

Simple fix Dariusz =)

The ' ' quotes you have inside the header() call are constant quotes, this
means that $foo is _literally_ $foo rather than the _contents_ of $foo.
This is what you're seeing and why you're seeing it. Try:
header("Content-Disposition: attachment; filename=$Name");

or

header('Content-Disposition: attachment; filename=' . $Name);
The echo statement will do nothing useful for you.. just FWIW =)

Hope this helps.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a
Dariusz wrote:
...
The information for file locations etc is pulled from a database. What I
would like to do however is to be able to pull the filename from the
database and insert it as the filename, ie:

header('Content-Disposition: attachment; filename="$Name"');
Read the strings reference
http://www.php.net/manual/en/language.types.string.php

taking particular attention to what is and is not interpolated.

$name = 'Pedro';
$name2 = 'my name is ' . $name; // my name is Pedro
$name3 = 'my name is $name'; // my name is $name
$name4 = "my name is $name"; // my name is Pedro
$name5 = 'my name is "$name"'; // my name is "$name"
$name6 = "my name is '$name'"; // my name is 'Pedro'
But when I try to execute this, the "save as" box asks to save the file
$Name, and if I change the line to:

header('Content-Disposition: attachment; filename=$Name');


try

header('Content-Disposition: attachment; filename="' . $Name . '"');

which will output something like
Content-Disposition: attachment; filename="test.scr"
No garanties this will work as you want on all browsers though :)
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

P: n/a
In article <pa****************************@bubbleboy.digiserv .net>, "Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
The ' ' quotes you have inside the header() call are constant quotes, this
means that $foo is _literally_ $foo rather than the _contents_ of $foo.
This is what you're seeing and why you're seeing it. Try:
header("Content-Disposition: attachment; filename=$Name");

or

header('Content-Disposition: attachment; filename=' . $Name);
The echo statement will do nothing useful for you.. just FWIW =)

Hope this helps.


Thanks Ian, this worked perfectly.

I didn't spot the problem as I have not worked with the headers before, so
I thought that the quote marks had to be the way I showed them in the
example (I got them off some site showing examples of the use of the
headers).

Dariusz
Jul 17 '05 #4

P: n/a
In article <c4*************@ID-203069.news.uni-berlin.de>, Pedro Graca <he****@hotpop.com> wrote:
taking particular attention to what is and is not interpolated.

$name = 'Pedro';
$name2 = 'my name is ' . $name; // my name is Pedro
$name3 = 'my name is $name'; // my name is $name
$name4 = "my name is $name"; // my name is Pedro
$name5 = 'my name is "$name"'; // my name is "$name"
$name6 = "my name is '$name'"; // my name is 'Pedro'
But when I try to execute this, the "save as" box asks to save the file
$Name, and if I change the line to:

header('Content-Disposition: attachment; filename=$Name');


try

header('Content-Disposition: attachment; filename="' . $Name . '"');

which will output something like
Content-Disposition: attachment; filename="test.scr"
No garanties this will work as you want on all browsers though :)


Thanks for the information Pedro. I was confused as it's the first time
working with the headers, and the example I found was not clear at all (on
the use of quote marks). The changes now work.

Thanks.

Dariusz
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.