Не удается передать переменные в заголовок содержимого-расположение PHP
У меня странная ситуация при передаче переменных в заголовке Content-Disposition. Заголовок не смог прочитать какую-либо переменную при передаче в качестве имени файла. Но он может читать, когда имя файла не передается как $ variable.
Этот код работает:
header('Content-Disposition: attachment; filename="uiiuyuuiuiyuiy.zip"');
header('Content-Disposition: attachment; filename="uii uyuui uiyuiy.zip"');
Этот код пропустит пустую строку:
$filename = 'uiiuyuuiuiyuiy.zip';
header('Content-Disposition: attachment; filename="'.$filename.'"');
То же самое с любыми переменными, которые я пытался передать как:
$code = strtolower(str_replace(' ', '', $_GET['code'])).'.zip';
header('Content-Disposition: attachment; filename="'.$code.'"');
Я также попробовал некоторый возможный синтаксис:
header("Content-Disposition: attachment; filename=$code");
header('Content-Disposition: attachment; filename='.$code);
header("Content-Disposition: attachment; filename=".$code);
header("Content-Disposition: attachment; filename='".$code."'");
header('Content-Disposition: attachment; filename="$filename"');
Мой полный код:
$down = '/path/to/zip/file.zip';
$code = strtolower(str_replace(' ', '', $_GET['code'])).'.zip';
function downloadTest($zip) {
if(file_exists($zip)) {
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$code.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($zip));
ob_clean();
flush();
readfile($zip);
exit;
}
}
downloadTest($down);
Моя версия PHP 7.0 Apache 2.4
Любая помощь высоко ценится.