How to get the site root directory under PHP

WBOY
Release: 2016-07-29 08:59:08
Original
1597 people have browsed it
<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[&#39;PHP_SELF&#39;] ? $_SERVER[&#39;PHP_SELF&#39;] : $_SERVER[&#39;SCRIPT_NAME&#39;];
  
$url=&#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].substr($PHP_SELF,0,strrpos($PHP_SELF, &#39;/&#39;)+1);
  
echo $url;
  
方法3:
  
$basepath=$_SERVER[&#39;PHP_SELF&#39;];
  
$basepath=substr($basepath,0,strpos($basepath,"文件夹名称"));
  
echo $basepath;
  
如:你把文件保存为a.php并路径为:/wjj/wjj1/wjj2/a.php
  
上面的例子就写成:
  
$basepath=$_SERVER[&#39;PHP_SELF&#39;];
  
$basepath=substr($basepath,0,strpos($basepath,"wjj1"));
  
echo $basepath;
  
方法4:
  
$basepath=$_SERVER[&#39;PHP_SELF&#39;];
  
preg_match("/(\/)?([^\/]+)/",$basepath,$wjm);
  
echo $wjm[0];
  
注意,第二种方法我不太会,如文件路径为:/wjj/wjj1/wjj2/a.php,它只返回 /wjj 后面的/自己加!
  
?></strong></span>
Copy after login
Original address: http://zhidao.baidu.com/link?url=IvUdqFmHVOwdLwY38k1cLazpf4BB7PR4MYoLwlpBkgzMtf_Jpb9U7l0JCjPhq6lTVVRCPUf1l-oEurWfmlwgvnkixhBytpLJtYliSOncNeO

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template