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

PHP获取站点源目录

Jun 13, 2016 pm 12:21 PM
echo php self server

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> ?>
Copy after login

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles