Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何在内容处置中设置完整目录路径?_Php_Content Disposition - Fatal编程技术网

PHP-如何在内容处置中设置完整目录路径?

PHP-如何在内容处置中设置完整目录路径?,php,content-disposition,Php,Content Disposition,我正在向下载页传递文件名。 即somefile.xls 下载页面将完整目录路径添加回文件名。 ie c:\temp\somefile.xls 问题是现在设置标题的“内容配置”不起作用。它要下载的文件名是完整目录文件名路径。 ie c\u temp\u somefile 内容处置能否处理完整路径 如果可以,我如何让我的脚本正确下载文件 代码是: $myad = $_GET['myad']; $glob_string = realpath('/foldera/folderb/folderc').

我正在向下载页传递文件名。
即somefile.xls

下载页面将完整目录路径添加回文件名。
ie c:\temp\somefile.xls

问题是现在设置标题的“内容配置”不起作用。它要下载的文件名是完整目录文件名路径。 ie c\u temp\u somefile

内容处置能否处理完整路径

如果可以,我如何让我的脚本正确下载文件

代码是:

$myad = $_GET['myad'];
$glob_string =  realpath('/foldera/folderb/folderc'). DIRECTORY_SEPARATOR .$myad;

header('Content-Type: application/excel');
$headerstring = 'Content-Disposition: attachment; filename='.$glob_string;
header($headerstring);
readfile($myad);
更新代码(来自答案):


从来没有。如果浏览器接受完整路径,则是时候快速提交错误:这将是一个主要的安全漏洞。

不要通过标题字符串传递完整路径,而是使用基本名称(
$myad

您确实应该对
$\u GET['myad']
使用更好的验证,因为您的脚本将向用户传递任意路径(
readfile()
获取未过滤的用户输入)。这是一个安全漏洞

使用
realpath
计算实际路径,确保文件位于允许的文件夹内,然后在完整路径上使用
basename()
获取普通文件名。通过
内容处置
头传递此子字符串,但使用
readfile()的实际路径


更新:更新后的代码仍然存在安全漏洞。如果
$\u获得包含
./../some/full/path
的['myad']
,脚本将很乐意将任何请求的可读文件发送到客户端

您应该使用以下代码段中的内容:

$myad = $_GET['myad'];

$rootDir = realpath('/mit/mit_tm/mrl_bol');
$fullPath = realpath($rootDir . '/' . $myad);

// Note that, on UNIX systems, realpath() will return false if a path
// does not exist, but an absolute non-existing path on Windows.
if ($fullPath && is_readable($fullPath) && dirname($fullPath) === $rootDir) {
    // OK, the requested file exists and is in the allowed root directory.
    header('Content-Type: application/excel');
    // basename() returns just the file name.
    header('Content-Disposition: attachment; filename=' . basename($fullPath));
    readfile($fullPath);
}

您可以在
内容处置
标题中放入几乎所有您想要的内容,但出于安全原因,大多数浏览器会忽略或替换路径,并将它们转换为运行它们的操作系统的有效文件名

内容配置
只是对浏览器的提示,web客户端不必遵守此设置


因此,不可以,您不能强制下载到客户端计算机上的特定目录。

我不知道这是否有帮助,但我认为excel文档的内容类型标题可能不太正确,我自己也没有尝试过,但那些microsoft包很快就来了,像mirosoft一样,word是
应用程序/vnd.openxmlformats of cedocument.wordprocessingml.document

为什么会被否决?这看起来确实是一个重大的安全漏洞。Ferdinand是对的,这是一个巨大的安全漏洞:恶意用户几乎可以下载web服务器可以访问的任何东西(例如数据库密码)。但是,关于内容处置标头,即使具有有效路径,浏览器仍将忽略它。是的。事实上,HTTP 1.1规范明确地从内容处置头中排除了目录路径信息:+1-谢谢。有道理-我的代码是针对内部网站的,因此安全漏洞不太重要。即使是针对内部网站,我也会遵循Ferdinand的建议:避免漏洞比以后修补漏洞要好得多:)
$myad = $_GET['myad'];

$rootDir = realpath('/mit/mit_tm/mrl_bol');
$fullPath = realpath($rootDir . '/' . $myad);

// Note that, on UNIX systems, realpath() will return false if a path
// does not exist, but an absolute non-existing path on Windows.
if ($fullPath && is_readable($fullPath) && dirname($fullPath) === $rootDir) {
    // OK, the requested file exists and is in the allowed root directory.
    header('Content-Type: application/excel');
    // basename() returns just the file name.
    header('Content-Disposition: attachment; filename=' . basename($fullPath));
    readfile($fullPath);
}