<span style="font-size:18px;"><strong>方法1: 在global.inc 里定义根目录 define("APP_ROOT",dirname(__FILE__)); 在任何PHP文件中可以引用该常量 require_once(APP_ROOT."/inc/head.php"); 方法2: <?php $PHP_SELF=$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']; $url='http://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF, '/')+1); echo $url; 方法3: $basepath=$_SERVER['PHP_SELF']; $basepath=substr($basepath,0,strpos($basepath,"文件夹名称")); echo $basepath; 如:你把文件保存为a.php并路径为:/wjj/wjj1/wjj2/a.php 上面的例子就写成: $basepath=$_SERVER['PHP_SELF']; $basepath=substr($basepath,0,strpos($basepath,"wjj1")); echo $basepath; 方法4: $basepath=$_SERVER['PHP_SELF']; preg_match("/(\/)?([^\/]+)/",$basepath,$wjm); echo $wjm[0]; 注意,第二种方法我不太会,如文件路径为:/wjj/wjj1/wjj2/a.php,它只返回 /wjj 后面的/自己加! ?></strong></span>
The above introduces how to get the site root directory under PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.