PHP使用一个变量下载文件


PHP using a variable in a file download

我有一个由运行的命令生成的变量。一切似乎都很好,我得到了一个文件大小正确的文件下载对话框。问题是我无法将我的变量作为实际的文件名。它总是$fullpath作为我的文件名

我已经尝试了许多不同的方式,包括无报价

 header('Content-Type: application/download');
    header('Content-Disposition: attachment; filename="$fullpath"');
    header("Content-Length: " . filesize("$fullpath"));
    $fp = fopen("$fullpath", "r");
    fpassthru($fp);
    fclose($fp);

PHP不解析单引号'中的变量。使用双引号",但也需要使用'"转义任何希望作为字符串一部分的双引号。

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

或者插入变量和字符串。

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

此外,在这些地方添加引号毫无用处:

filesize($fullpath);
fopen($fullpath);