Get parent directory of running script

后端 未结 13 2084
攒了一身酷
攒了一身酷 2020-11-30 04:20

In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:

$_         


        
13条回答
  •  余生分开走
    2020-11-30 04:55

    This is a function that I use. Created it once so I always have this functionality:

    function getDir(){
        $directory = dirname(__FILE__);
        $directory = explode("/",$directory);
        $findTarget = 0;
        $targetPath = "";
        foreach($directory as $dir){
            if($findTarget == 1){
                $targetPath = "".$targetPath."/".$dir."";
            }
            if($dir == "public_html"){
                $findTarget = 1;
            }
        }
        return "http://www.".$_SERVER['SERVER_NAME']."".$targetPath."";
    }
    

提交回复
热议问题