php - 你如何使用 PHP 检查文件是否在某个目录中?

在 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/

相关文章:

c# - 哪个类需要实现 INotifyDataErrorInfo?

google-apps-script - 将使用 google apps 脚本构建的图表插入到 go

css - 高度 :100%; works, 但最小高度 :100%; doesn't work?

c# - 向 XElement 添加空白行以保留其他格式

web-applications - 在 Servlet 中获取 Web 应用程序名称

smalltalk - 遍历圆变形的直径

c# - 是否可以在 MS Access 中执行批量插入

oauth - 刷新 OAuth2 访问 token 的正确范例

install4j - 如何在自定义代码或 API 中检查 32 位或 64 位

c# - XML Serializer 不序列化具有公共(public) getter 但没有 se