背景
我有一个在 CentOS 服务器上运行的 PHP 应用程序。前端使用 ajax 调用来调用 PHP 脚本。该脚本从服务器返回一个文件并将其下载到客户端。
问题
如果文件#
名中有 ,则文件下载失败。
例子
File#Name.pdf
= 没有正确下载
FileName.pdf
= 下载是否正确
这是用于检索文件的 PHP,
if( isset($_GET['path'])) {
$path = $_GET['path'];
if(!file_exists($path)) {
die('file not found');
} else {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$path);
header('Content-Length: '.filesize($path));
readfile($path);
ob_clean();
flush();
exit;
}
}
问题
为什么名称中的文件无法下载#
?
啊,文件名是否来自 GET 请求?如果是这样,# 可能会被重写为 %23。尝试
$path = urldecode($_GET['path']);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句