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');
?>
KPO
  • 890
  • 2
  • 20
  • 40

5 Answers5

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.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 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 Jul 28 '11 at 13:35
  • 1
    @KPO Well, you'll have to figure out what you're sending wrong, then. That's a different question. – lonesomeday Jul 28 '11 at 13:37
2

You don't need quotes here, just use

header('Content-disposition: attachment; filename='.$filename);
header('Content-type: '.$type);
readfile($filename);
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).

Griwes
  • 8,805
  • 2
  • 43
  • 70
1

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

minichate
  • 1,914
  • 13
  • 17
1

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

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176