gpt4 book ai didi

php - 如何将 $_GET 路径与 file_exists 一起使用并保证其安全?

转载 作者:可可西里 更新时间:2023-11-01 00:48:28 26 4
gpt4 key购买 nike

我有一个函数可以通过 jQuery 检查文件是否存在,它会调用 PHP 脚本,我将在单击索引页上的按钮更改某些图像时使用该脚本。

jQuery 函数:

function fileExists(path){
$.getJSON("/ajax/fileExists.php",{ path: path },
function (data){
return data.path;
});
}

文件存在.php:

$path=$_SERVER['DOCUMENT_ROOT'].'/packs'.$_GET['path'];

if(file_exists($path)){
echo json_encode(TRUE);
}else{
echo json_encode(FALSE);
}

我担心有人使用此脚本列出我可能不想让他们知道的服务器或文件的内容,因此我使用 DOCUMENT_ROOT 和/packs 来尝试限制对该目录的调用,但我认为人们可以简单地在提供的路径中使用 ../来检查替代方案。

确保此安全的最佳方法是什么,理想情况下将其限制在/packs,还有我应该担心的任何其他问题吗?

编辑:javascript/jQuery 中的示例调用:

if( fileExists('/index.php') ){
alert('Exists');
}else{
alert('Doesn\'t exist');
}

最佳答案

这是我过去处理它的方式:

$path = realpath($_SERVER['DOCUMENT_ROOT'].'/packs'.$_GET['path']);
if (strpos($path, $_SERVER['DOCUMENT_ROOT']) !== 0) {
//It's looking to a path that is outside the document root
}

关于php - 如何将 $_GET 路径与 file_exists 一起使用并保证其安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12655685/

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