Home >Backend Development >PHP Tutorial >PHP获取站点源目录

PHP获取站点源目录

WBOY
WBOYOriginal
2016-06-13 12:21:291244browse

PHP获取站点根目录

<span style="color: #008080;">  1</span> <span style="color: #000000;">将下面的代码放在网站根目录下的一个文件中,以便在其它文件中引用。</span><span style="color: #008080;">  2</span> <span style="color: #008080;">  3</span> <span style="color: #000000;">路径方式:</span><span style="color: #008080;">  4</span> <span style="color: #008080;">  5</span> <span style="color: #008080;">define</span>('BASE_PATH',<span style="color: #008080;">str_replace</span>('\\','/',<span style="color: #008080;">realpath</span>(<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>).'/'))."/"<span style="color: #000000;">);</span><span style="color: #008080;">  6</span> <span style="color: #008080;">  7</span> <span style="color: #0000ff;">echo</span><span style="color: #000000;"> BASE_PATH;</span><span style="color: #008080;">  8</span> <span style="color: #008080;">  9</span>  <span style="color: #008080;"> 10</span> <span style="color: #008080;"> 11</span> 输出结果:E:/wwwroot/<span style="color: #008080;"> 12</span> <span style="color: #008080;"> 13</span> <span style="color: #000000;">url方式:</span><span style="color: #008080;"> 14</span> <span style="color: #008080;"> 15</span>  <span style="color: #008080;"> 16</span> <span style="color: #008080;"> 17</span> <span style="color: #800080;">$PHP_SELF</span>=<span style="color: #800080;">$_SERVER</span>['PHP_SELF'<span style="color: #000000;">];</span><span style="color: #008080;"> 18</span> <span style="color: #008080;"> 19</span> <span style="color: #800080;">$url</span>='http://'.<span style="color: #800080;">$_SERVER</span>['HTTP_HOST'].<span style="color: #008080;">substr</span>(<span style="color: #800080;">$PHP_SELF</span>,0,<span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$PHP_SELF</span>,'/')+1<span style="color: #000000;">);</span><span style="color: #008080;"> 20</span> <span style="color: #008080;"> 21</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$url</span><span style="color: #000000;">;</span><span style="color: #008080;"> 22</span> <span style="color: #008080;"> 23</span>  <span style="color: #008080;"> 24</span> <span style="color: #008080;"> 25</span> 输出结果:http:<span style="color: #008000;">//</span><span style="color: #008000;">localhost/</span><span style="color: #008080;"> 26</span> <span style="color: #000000;">其它的资料整理:</span><span style="color: #008080;"> 27</span> <span style="color: #000000;">PHP取得站点根目录</span><span style="color: #008080;"> 28</span> <span style="color: #008080;"> 29</span>  <span style="color: #008080;"> 30</span> <span style="color: #008080;"> 31</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['HTTP_HOST'];<span style="color: #008000;">//</span><span style="color: #008000;">当前请求的 Host: 头部的内容 即域名信信息</span><span style="color: #008080;"> 32</span> <span style="color: #008080;"> 33</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['PHP_SELF'];<span style="color: #008000;">//</span><span style="color: #008000;">当前正在执行脚本的文件相对网站根目录地址,就算该文件被其他文件引用也可以正确得到地址</span><span style="color: #008080;"> 34</span> <span style="color: #008080;"> 35</span>  <span style="color: #008080;"> 36</span> <span style="color: #008080;"> 37</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['SCRIPT_NAME'];<span style="color: #008000;">//</span><span style="color: #008000;">当前正在执行脚本的文件相对网站根目录地址,但当该文件被其他文件引用时,只显示引用文件的相对地址,不显示该被引用脚本的相对地址。</span><span style="color: #008080;"> 38</span> <span style="color: #008080;"> 39</span>  <span style="color: #008080;"> 40</span> <span style="color: #008080;"> 41</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['DOCUMENT_ROOT'];<span style="color: #008000;">//</span><span style="color: #008000;">网站相对服务器地址即网站的绝对路径名 #当前运行脚本所在的文档根目录。在服务器配置文件中定义</span><span style="color: #008080;"> 42</span> <span style="color: #008080;"> 43</span>  <span style="color: #008080;"> 44</span> <span style="color: #008080;"> 45</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_SERVER</span>['SCRIPT_FILENAME'];<span style="color: #008000;">//</span><span style="color: #008000;">当前执行脚本的绝对路径名。</span><span style="color: #008080;"> 46</span> <span style="color: #008080;"> 47</span>  <span style="color: #008080;"> 48</span> <span style="color: #008080;"> 49</span> <span style="color: #000000;">方法一:</span><span style="color: #008080;"> 50</span> <span style="color: #008080;"> 51</span>  <span style="color: #008080;"> 52</span> <span style="color: #008080;"> 53</span> <span style="color: #000000;">今天讲了大致的站点目录结构,其中说到了怎样取得站点根目录,好多同学不是很理解,下面一步一步道来:</span><span style="color: #008080;"> 54</span> <span style="color: #008080;"> 55</span>  <span style="color: #008080;"> 56</span> <span style="color: #008080;"> 57</span> 我的站点目录据对路径:D:\www\message2011,测试文件路径:D:\www\message2011\<span style="color: #0000ff;">include</span>\config.inc.<span style="color: #000000;">php</span><span style="color: #008080;"> 58</span> <span style="color: #008080;"> 59</span>  <span style="color: #008080;"> 60</span> <span style="color: #008080;"> 61</span> 在根目录下新建一个文件,也可以隔一层目录创建,就以上面为例,我要在config.inc.<span style="color: #000000;">php中取得站点的根目录。</span><span style="color: #008080;"> 62</span> <span style="color: #008080;"> 63</span>  <span style="color: #008080;"> 64</span> <span style="color: #008080;"> 65</span> 1<span style="color: #000000;">、取得当前文件的路径:用魔术常量__FILE__。这里的路径包含了文件名,结果如下:</span><span style="color: #008080;"> 66</span> <span style="color: #008080;"> 67</span>  <span style="color: #008080;"> 68</span> <span style="color: #008080;"> 69</span> D:\www\message2011\<span style="color: #0000ff;">include</span>\config.inc.<span style="color: #000000;">php</span><span style="color: #008080;"> 70</span> <span style="color: #008080;"> 71</span>  <span style="color: #008080;"> 72</span> <span style="color: #008080;"> 73</span> 2、去掉上面路径的文件名,得到纯路径:<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span><span style="color: #000000;">),结果如下:</span><span style="color: #008080;"> 74</span> <span style="color: #008080;"> 75</span>  <span style="color: #008080;"> 76</span> <span style="color: #008080;"> 77</span> D:\www\message2011\<span style="color: #0000ff;">include</span><span style="color: #008080;"> 78</span> <span style="color: #008080;"> 79</span>  <span style="color: #008080;"> 80</span> <span style="color: #008080;"> 81</span> 3、返回一层目录到根目录:<span style="color: #008080;">realpath</span>(<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>).'/../'<span style="color: #000000;">),结果如下:</span><span style="color: #008080;"> 82</span> <span style="color: #008080;"> 83</span>  <span style="color: #008080;"> 84</span> <span style="color: #008080;"> 85</span> D:<span style="color: #000000;">\www\message2011</span><span style="color: #008080;"> 86</span> <span style="color: #008080;"> 87</span>  <span style="color: #008080;"> 88</span> <span style="color: #008080;"> 89</span> 4、把向左的”\“转化成向右的”/“:<span style="color: #008080;">str_replace</span>( '\\' , '/' , <span style="color: #008080;">realpath</span>(<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>).'/../'<span style="color: #000000;">)),结果如下:</span><span style="color: #008080;"> 90</span> <span style="color: #008080;"> 91</span>  <span style="color: #008080;"> 92</span> <span style="color: #008080;"> 93</span> D:/www/<span style="color: #000000;">message2011</span><span style="color: #008080;"> 94</span> <span style="color: #008080;"> 95</span>  <span style="color: #008080;"> 96</span> <span style="color: #008080;"> 97</span> 到这就是我们想要的站点根目录了,可以根据需求设定第三步返回目录层数,比如:返回2层就是 /../../,那么就会得到 D:<span style="color: #000000;">\www\,以此类推。</span><span style="color: #008080;"> 98</span> <span style="color: #008080;"> 99</span>  <span style="color: #008080;">100</span> <span style="color: #008080;">101</span> <span style="color: #000000;">方法二:</span><span style="color: #008080;">102</span> <span style="color: #008080;">103</span>  <span style="color: #008080;">104</span> <span style="color: #008080;">105</span> 在global.<span style="color: #000000;">inc   里定义根目录</span><span style="color: #008080;">106</span> <span style="color: #008080;">107</span>  <span style="color: #008080;">108</span> <span style="color: #008080;">109</span> <span style="color: #008080;">define</span>("APP_ROOT",<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span><span style="color: #000000;">));</span><span style="color: #008080;">110</span> <span style="color: #008080;">111</span>  <span style="color: #008080;">112</span> <span style="color: #008080;">113</span> <span style="color: #000000;">在任何PHP文件中可以引用该常量</span><span style="color: #008080;">114</span> <span style="color: #008080;">115</span>  <span style="color: #008080;">116</span> <span style="color: #008080;">117</span> <span style="color: #0000ff;">require_once</span>(APP_ROOT."/inc/head.php"<span style="color: #000000;">);</span><span style="color: #008080;">118</span> <span style="color: #008080;">119</span>  <span style="color: #008080;">120</span> <span style="color: #008080;">121</span>  <span style="color: #008080;">122</span> <span style="color: #008080;">123</span> <span style="color: #000000;">方法三:</span><span style="color: #008080;">124</span> <span style="color: #008080;">125</span>  <span style="color: #008080;">126</span> <span style="color: #008080;">127</span> <span style="color: #000000;">php</span><span style="color: #008080;">128</span> <span style="color: #008080;">129</span>  <span style="color: #008080;">130</span> <span style="color: #008080;">131</span> <span style="color: #800080;">$PHP_SELF</span>=<span style="color: #800080;">$_SERVER</span>['PHP_SELF'] ? <span style="color: #800080;">$_SERVER</span>['PHP_SELF'] : <span style="color: #800080;">$_SERVER</span>['SCRIPT_NAME'<span style="color: #000000;">];</span><span style="color: #008080;">132</span> <span style="color: #008080;">133</span>  <span style="color: #008080;">134</span> <span style="color: #008080;">135</span> <span style="color: #800080;">$url</span>='http://'.<span style="color: #800080;">$_SERVER</span>['HTTP_HOST'].<span style="color: #008080;">substr</span>(<span style="color: #800080;">$PHP_SELF</span>,0,<span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$PHP_SELF</span>, '/')+1<span style="color: #000000;">);</span><span style="color: #008080;">136</span> <span style="color: #008080;">137</span>  <span style="color: #008080;">138</span> <span style="color: #008080;">139</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$url</span><span style="color: #000000;">;</span><span style="color: #008080;">140</span> <span style="color: #008080;">141</span>  <span style="color: #008080;">142</span> <span style="color: #008080;">143</span> <span style="color: #000000;">方法四:</span><span style="color: #008080;">144</span> <span style="color: #008080;">145</span>  <span style="color: #008080;">146</span> <span style="color: #008080;">147</span> <span style="color: #800080;">$basepath</span>=<span style="color: #800080;">$_SERVER</span>['PHP_SELF'<span style="color: #000000;">];</span><span style="color: #008080;">148</span> <span style="color: #008080;">149</span>  <span style="color: #008080;">150</span> <span style="color: #008080;">151</span> <span style="color: #800080;">$basepath</span>=<span style="color: #008080;">substr</span>(<span style="color: #800080;">$basepath</span>,0,<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$basepath</span>,"文件夹名称"<span style="color: #000000;">));</span><span style="color: #008080;">152</span> <span style="color: #008080;">153</span>  <span style="color: #008080;">154</span> <span style="color: #008080;">155</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$basepath</span><span style="color: #000000;">;</span><span style="color: #008080;">156</span> <span style="color: #008080;">157</span>  <span style="color: #008080;">158</span> <span style="color: #008080;">159</span> 如:你把文件保存为a.php并路径为:/wjj/wjj1/wjj2/a.<span style="color: #000000;">php</span><span style="color: #008080;">160</span> <span style="color: #008080;">161</span>  <span style="color: #008080;">162</span> <span style="color: #008080;">163</span> <span style="color: #000000;">上面的例子就写成:</span><span style="color: #008080;">164</span> <span style="color: #008080;">165</span>  <span style="color: #008080;">166</span> <span style="color: #008080;">167</span> <span style="color: #800080;">$basepath</span>=<span style="color: #800080;">$_SERVER</span>['PHP_SELF'<span style="color: #000000;">];</span><span style="color: #008080;">168</span> <span style="color: #008080;">169</span>  <span style="color: #008080;">170</span> <span style="color: #008080;">171</span> <span style="color: #800080;">$basepath</span>=<span style="color: #008080;">substr</span>(<span style="color: #800080;">$basepath</span>,0,<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$basepath</span>,"wjj1"<span style="color: #000000;">));</span><span style="color: #008080;">172</span> <span style="color: #008080;">173</span>  <span style="color: #008080;">174</span> <span style="color: #008080;">175</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$basepath</span><span style="color: #000000;">;</span><span style="color: #008080;">176</span> <span style="color: #008080;">177</span>  <span style="color: #008080;">178</span> <span style="color: #008080;">179</span> <span style="color: #000000;">方法五:</span><span style="color: #008080;">180</span> <span style="color: #008080;">181</span>  <span style="color: #008080;">182</span> <span style="color: #008080;">183</span> <span style="color: #800080;">$basepath</span>=<span style="color: #800080;">$_SERVER</span>['PHP_SELF'<span style="color: #000000;">];</span><span style="color: #008080;">184</span> <span style="color: #008080;">185</span>  <span style="color: #008080;">186</span> <span style="color: #008080;">187</span> <span style="color: #008080;">preg_match</span>("/(\/)?([^\/]+)/",<span style="color: #800080;">$basepath</span>,<span style="color: #800080;">$wjm</span><span style="color: #000000;">);</span><span style="color: #008080;">188</span> <span style="color: #008080;">189</span>  <span style="color: #008080;">190</span> <span style="color: #008080;">191</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$wjm</span>[0<span style="color: #000000;">];</span><span style="color: #008080;">192</span> <span style="color: #008080;">193</span>  <span style="color: #008080;">194</span> <span style="color: #008080;">195</span> 注意,第二种方法我不太会,如文件路径为:/wjj/wjj1/wjj2/a.php,它只返回 /wjj 后面的/<span style="color: #000000;">自己加!</span><span style="color: #008080;">196</span> <span style="color: #008080;">197</span>  <span style="color: #008080;">198</span> <span style="color: #008080;">199</span> ?>

 

Statement:
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