微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。