views:

86

answers:

7

I am trying to print out the value of $name

yet it keeps printing out $name instead of the value here is the line:

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

Try like this:

header('Content-Disposition: attachment; filename='.$name);
Parkyprg
this will break if $name contains spaces, hence why his original code had it wrapped in double quotes.
Spudley
A: 
header('Content-Disposition: attachment; filename='.$name);
lfx
You got an extra `'` at before the closing parenthesis.
Ruel
It just mistype.
lfx
+2  A: 

String interpolation does not happen between single quotes.

$name = "test";
echo "Value: $name"; // Value: test
echo 'Value: $name'; // Value: $name

More details here.

Victor Nicollet
+4  A: 

PHP will only evaluate variables within double-quoted strings, not single quoted strings like you've used in your example.

Try this instead (if you need to output double quotes within a double-quoted string, you need to escape them with a backslash, otherwise PHP will treat them as the end-of-string delimiter):

header("Content-Disposition: attachment; filename=\"$name\"");
Ty W
that did not work that printed the name of the php page.
Matthew Carter
ORIGNINAL: header('Content-Disposition: attachment; filename="HELLO"');
Matthew Carter
i am trying to make it so there is a variable where hello is but the filename command needs double quotes
Matthew Carter
@Matthew what it was printed is another question. You should ask "How to perform a manual download" instead of this silly syntax question.
Col. Shrapnel
+1 for explaining the difference between single and double quotes
steven_desu
+1  A: 

When dealing with single-quoted strings, always concatenate variables.

header('Content-Disposition: attachment; filename="' . $name . '"');
Ruel
but filename needs double quotes and needs another one at the end
Matthew Carter
Yep, there is :)
Ruel
+2  A: 

I think this is what your looking for

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

This would give a header like so:

Content-Disposition: attachment; filename="some_file_name.ext"

Hope this helps

Another way to do this is using sprintf

$header = sprintf('Content-Disposition: attachment; filename="%s"',$name);
header($header);

The sprintf is not really needed but there to show you how the variable would be placed into the header string

RobertPitt
neither one worked
Matthew Carter
I do this practically every day, I assure you my methods are correct, more than likely your variable is null or incorrect. Please show more code, Also if it was you who down voted me, can you please restrain from down voting answers that answer your question, if the answer did not work i assure you its another section of your code.
RobertPitt
A: 

Why don't you just use " instead of ' like :

header("Content-Disposition: attachment; filename=$name");
Braveyard