如何在php中更改Content-Disposition的目录

问题描述

我只想将我的实时 sql 数据导出到特定目录中的 excel 文件中。我能够从 sql 下载 excel 文件,但它正在系统的下载文件夹中下载,但我想在实时服务器“/home/jw07sp1ptrfw/public_html/”上下载它,这是我在实时服务器上的目录。但是当我运行此代码,它会在系统下载文件夹中下载文件 home_jw07sp1ptrfw_public_html_report.xls。如果有人可以提前感谢,请帮助我

 include('config.PHP');
    $html='<table> <tr><td> Name</td><td>Email</td><td>Phone</td></tr>';
    
      $result = MysqLi_query($link,"SELECT * FROM `order`");
                              while($row = MysqLi_fetch_assoc($result))
                                    {
    
    $html.='<tr><td>'.$row["name"].'</td><td>'.$row["email"].' </td><td> '.$row["phone"].'</td></tr>';
    
                                    }
                                    
    $html.='</table>';
    header('Content-Type: application/xls');
    header('Content-disposition: attachment; filename= /home/jw07sp1ptrfw/public_html/report.xls');
     echo $html;

我也试过这个。但没有得到解决方案。

$file_name = "report.xls";

$rootDir = realpath("/home/jw07sp1ptrfw/public_html");
$fullPath = realpath($rootDir . "/" . $file_name);

// if ($fullPath && is_readable($fullPath) && dirname($fullPath) === $rootDir) 
{
    
    header('Content-Type: application/xls');
    header('Content-disposition: attachment; filename=' . basename($fullPath));
    readfile($fullPath);
     echo $html;
}

解决方法

你不能那样做。文件名只是对浏览器的一个提示,出于安全原因,它将对其进行预处理。见https://greenbytes.de/tech/webdav/rfc6266.html#disposition.parameter.filename