2

I am trying to get php variables inserted into the code below. When the document download pop up shows up it shows $filename instead of the actual file name. How can i achieve this?

code:

<?php
header('Content-disposition: attachment; filename=$filename');
header('Content-type: $type');
readfile('$filename');
?>

5 Answers 5

3

You're using single quotes. Variables inside string literals using single quotes are not evaluated. Use concatenation or double quotes instead.

<?php
header("Content-disposition: attachment; filename=$filename"); // double quotes
header('Content-type: ' . $type); // concatenation
readfile($filename); // no quotes needed
?>

See the PHP manual page for the String type.

Sign up to request clarification or add additional context in comments.

2 Comments

it works but now i get this error: Couldn’t open the file. It may be corrupt or a file format that Preview doesn’t recognize.
@KPO Well, you'll have to figure out what you're sending wrong, then. That's a different question.
2

You don't need quotes here, just use

header('Content-disposition: attachment; filename='.$filename);
header('Content-type: '.$type);
readfile($filename);

Comments

1

Use ", not ', to get variables' values instead of names. When you use ", variables are parsed (to parse array elements it is required to use { and }, but I'm sure you'll find it out when you will need it).

Comments

1

Use double-quotes: " instead of single quotes: '

Comments

1

Use " quotes. PHP doesn't substitute variables in a string enclosed in ' quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.