php realpath() returns a blank string

半世苍凉 提交于 2020-01-17 12:27:10

问题


Guided by this post Preventing Directory Traversal in PHP but allowing paths I am trying to prevent directory traversal, but I am experiencing something odd, in the first instance realpath returns a good path

        $fPath = $path.$parent.'/'.$name;
//$name (the user input) in this sample is "fff", $parent is an empty string        

        //make sure user didn't try to traverse backwards on the basepath           
        $basepath = $path;
        $realBase = realpath($basepath);
echo $realBase."<br/>";
//gives: /Users/me/Documents/www/gallery/php_sample/uploadedImages

        $userpath = $fPath;
echo $userpath."<br/>";
//gives: /Users/me/Documents/www/gallery/php_sample/uploadedImages/fff

        $realUserPath = realpath($userpath);
echo $realUserPath."<br/>";
//gives blank (an empty string).

Any idéas on why?


回答1:


Like AbraCadaver said in comment, "This only works if the dir exists. To validate/sanitize the user supplied dir you will need some rules and clean it or reject it."



来源:https://stackoverflow.com/questions/29419037/php-realpath-returns-a-blank-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!