gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:18:41 30 4
gpt4 key购买 nike

在 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';
}

关于php - 你如何使用 PHP 检查文件是否在某个目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816749/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com