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"');
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"');
Try like this:
header('Content-Disposition: attachment; filename='.$name);
String interpolation does not happen between single quotes.
$name = "test";
echo "Value: $name"; // Value: test
echo 'Value: $name'; // Value: $name
More details here.
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\"");
When dealing with single-quoted
strings, always concatenate variables.
header('Content-Disposition: attachment; filename="' . $name . '"');
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
Why don't you just use "
instead of '
like :
header("Content-Disposition: attachment; filename=$name");