在 PHP 中访问文件时,可以使用“..”从目录中转义,这会导致安全风险。有什么方法可以验证文件是否在指定目录中?似乎没有内置函数。
最佳答案
这不是检查文件是否存在于预期位置的安全方法。您应该执行以下操作。
$base = '/expected/path/';
$filename = realpath($filename);
if ($filename === false || strncmp($filename, $base, strlen($base)) !== 0) {
echo 'Missing file or not in the expected location';
}
else {
echo 'The file exists and is in the expected location';
}
https://stackoverflow.com/questions/11816749/