note 83705 deleted from function.realpath by peehaa

From: Date: Sun, 28 Apr 2019 09:49:18 +0000
Subject: note 83705 deleted from function.realpath by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210714@lists.php.net to get a copy of this message
Note Submitter: waldemar dot axdorph at gmail dot com ---- I made this function so that I could see if a user was trying to go above the current directory. My top dir: /var/www/dir/ User wanted /var/www/ This comes in handy when you're making a php explorer/filemanager and you want your files in the directory above to be private. Here it is <?php //usage //$dir is the dir you are in //$dir_top is the highest dir allowed. //example /* if(above_dir('../', '/var/www/filemgr')) die('Not allowed. Please stick to your directories'); */ function above_dir($dir, $dir_top){ if($dir == $dir_top) return false; $dir = realpath($dir); $dir_top = realpath($dir_top); $dir = count(explode('/', $dir)); $dir_top = count(explode('/', $dir_top)); if($dir <= $dir_top){ return true; }else{ return false; } } ?>

« previous php.notes (#210714) next »