我正在通过php脚本下载文件,除了一个丑陋的事实外,一切都很完美 . 下载的文件保持相同的URL并附加原始名称 . 文件下载时如何保持相同的文件名?
http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx
if (isset($_GET['mfile'])) {
$file = $_SERVER['DOCUMENT_ROOT'].'/oc-content/plugins/mmailer/pfile/'.$_GET['mfile'];
if (file_exists($file) && is_readable($file) && preg_match('/\.docx$/',$file)) {
header('Content-Type: application/docx');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
/*
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($file));*/
}
else
{
header("HTTP/1.0 404 Not Found");
echo "Error 404: File Not Found: $file";
}
3 回答
header('Content-Disposition: attachment; filename='.basename($file));
这条线也许可以解决这个问题 .
header('Content-Disposition: attachment; filename="name_of_file_here"')
本来可以做到的 . 由于$ file包含完整路径,因此您将文件的完整路径作为header("Content-Disposition: attachment; filename=\"$file\"");
传递 . 而只是发送文件的名称 .