MKDIR 3 levels above using php -


level1/level2/level3/cwd/mkdir.php 

name of level1 not known; user specified & can anything. name of level2 & level3 known , remain static. current working directory contains mkdir.php file required create directory in level1 user provided name. mkdir.php file below job, don't know whether it's correct way. want experts approve , advice. in advance.

<?php     if (isset($_post['name']))     {        $newdir = $_post['name'];     $dirname = "..\\$newdir";         $step1 = "..\\cwd";          $step2 = "..\\$step1";           $step3 = "..\\$step2\\$dirname";          if (mkdir($step3, 0777, true))         {         echo "dir created successfully";         }         else         {         echo  "dir not created";         }     } ?> 

if you're running mkdir.php can use dirname() consecutively until reach level1.

also, it's important sanitize input data prevent malicious users creating "rogue" directories on system.

$name = filter_input(input_post, 'name', filter_validate_regexp, array('options' => array(     'regexp' => '/^\w+$/',     'flags' => filter_null_on_failure, )));  if (!is_null($name)) {     $base = dirname(dirname(dirname(__dir__)));     //      level1  level2  level3  cwd     $path = sprintf('%s/%s', $base, $name);      mkdir($path, 0777, true); } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -