Sanitize file path in PHP

后端 未结 7 1055
夕颜
夕颜 2020-12-01 11:00

Greetings, I\'m hoping to make my tiny program secure so that potential malicious users cannot view sensitive files on the server.

    $path = \"/home/gsmcm         


        
7条回答
  •  隐瞒了意图╮
    2020-12-01 11:13

    To strip all /. /.. or \. \.. and convert to all forward slash because the different environments will accept forward slash. This should provide a fairly safe filter for path input. In your code you should be comparing it to parent directories that you do not want access just in case.

     $path = realpath(implode('/', array_map(function($value) {return trim($value, '.');}, explode('/', str_replace('\\', '/', $path)))));  
    

提交回复
热议问题