Php:从服务器下载时保持相同的文件名

Php:从服务器下载时保持相同的文件名,php,Php,我正在通过php脚本下载一个文件,除了一个丑陋的事实外,其他一切都很好地工作。下载的文件保持相同的url并附加原始名称。下载文件时如何保持相同的文件名 http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx 如果(i

我正在通过php脚本下载一个文件,除了一个丑陋的事实外,其他一切都很好地工作。下载的文件保持相同的url并附加原始名称。下载文件时如何保持相同的文件名

http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx http://bachday.com/index.php?page=custom&file=mmailer/download.php?mfile=sample.docx 如果(isset($_GET['mfile'])){ $file=$\u SERVER['DOCUMENT\u ROOT']./oc content/plugins/mmailer/pfile/'.$\u GET['mfile']; 如果(文件存在($file)&&U可读($file)&&preg\u匹配('/\.docx$/',$file)){ 标题(“内容类型:应用程序/docx”); 标题(“内容处置:附件;文件名=\”$file\”); readfile($file); /* 标题(“到期日:0”); 标头(“缓存控制:无缓存,必须重新验证”); 标题(“杂注:无缓存”); echo(readfile($file))*/ } 其他的 { 标题(“未找到HTTP/1.0 404”); echo“错误404:找不到文件:
$File”; }
header('Content-Disposition:attachment;filename=“name\u of_file\u here””)
会成功的。您将文件的完整路径作为
header(“Content-Disposition:attachment;filename=\“$file\”);
传递到那里,因为您的$file包含完整路径。只需发送文件名。

header('Content-Disposition:attachment;filename=“name\u of_file\u here””)
会成功的。您将文件的完整路径作为
头传递到那里(“Content-Disposition:attachment;filename=\“$file\”);
,因为您的$file包含完整路径。只需发送文件名即可。

头('Content-Disposition:attachment;filename='.basename($file));

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=\"".basename($file)."\"");//use basename to extract filename from full file path
    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";
    }
这一行也许可以解决这个问题。

标题('Content-Disposition:attachment;filename='。basename($file));


这行代码也许可以解决这个问题。

试试这段代码。
标题(“内容处置:附件;文件名={$name}.{$file\u ending}”);
区别在于连接。试试这段代码。
标题(“内容处置:附件;文件名={$name}.{$file\u ending}”);
区别在于连接。
“…文件名=\”basename($file)\“”“
并不像您想象的那样。很抱歉,这是我的错误…忘记将函数保留在引号中。现在您可以在服务器中使用原始文件名并将其发送到下载的文件
“…文件名=\”basename($file)\“”
没有按您认为的那样做。很抱歉,这是我的错误…忘记将函数保留在引号中。现在您可以在服务器中使用原始文件名并将其发送到下载的文件。请解释此代码如何解决此问题。basename($file)将返回最后一部分(文件名和扩展名)请解释这段代码如何解决这个问题。basename($file)将返回$file字符串的最后一部分(文件名和扩展名),这是上面所问的问题。
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=\"".basename($file)."\"");//use basename to extract filename from full file path
    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";
    }