我有一个php脚本,它通过$_GET
获取相对路径名,读取该文件并创建它的缩略图。我不希望用户能够从服务器读取任何文件。只有来自某个目录的文件应该被允许,否则剧本应该exit()
。
这里是我的文件夹结构:
files/ <-- all files from this folder are public
my_stuff/ <-- this is the folder of my script that reads the files
我的脚本通过mydomain.com/my_stuff/script.php?pathname=files/some.jpg
访问。什么不应该被允许e。摹:mydomain.com/my_stuff/script.php?pathname=files/../db_login.php
所以,这里是脚本的文件夹my_stuff
相关部分:
...
$pathname = $_GET['pathname'];
$pathname = realpath('../' . $_GET['pathname']);
if(strpos($pathname, '/files/') === false) exit('Error');
...
我真的不知道有关的做法,似乎并不对我来说是安全的。任何人有更好的主意?
任何想法如何以编程方式设置$ basedir和$允许脚本在具有不同路径的不同服务器之间移植? – Max 2010-06-08 13:26:42
@Max啊欢迎来到我的回答:) – 2010-06-08 13:27:21