2

我正在尝试将 php 变量插入到下面的代码中。当文档下载弹出窗口出现时,它显示 $filename 而不是实际的文件名。我怎样才能做到这一点?

代码:

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

5 回答 5

4

您正在使用单引号。不评估使用单引号的字符串文字中的变量。请改用连接或双引号。

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

有关 String 类型,请参见 PHP 手册页

于 2011-07-28T13:32:38.530 回答
2

使用", not'来获取变量的值而不是名称。使用 时",会解析变量(解析数组元素需要使用 { 和 },但我相信您会在需要时找到它)。

于 2011-07-28T13:32:25.597 回答
2

使用双引号:"而不是单引号:'

于 2011-07-28T13:32:58.030 回答
2

使用"引号。PHP 不会替换用'引号括起来的字符串中的变量。

于 2011-07-28T13:33:01.353 回答
2

您在这里不需要引号,只需使用

header('Content-disposition: attachment; filename='.$filename);
header('Content-type: '.$type);
readfile($filename);
于 2011-07-28T13:33:38.853 回答