Heim Backend-Entwicklung 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> ?>
Nach dem Login kopieren

 

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

<🎜>: Bubble Gum Simulator Infinity - So erhalten und verwenden Sie Royal Keys
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusionssystem, erklärt
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Flüstern des Hexenbaum
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
<🎜> obscur: Expedition 33 - So erhalten Sie perfekte Chroma -Katalysatoren
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Java-Tutorial
1677
14
PHP-Tutorial
1278
29
C#-Tutorial
1257
24
PHP und Python: Verschiedene Paradigmen erklärt PHP und Python: Verschiedene Paradigmen erklärt Apr 18, 2025 am 12:26 AM

PHP ist hauptsächlich prozedurale Programmierung, unterstützt aber auch die objektorientierte Programmierung (OOP). Python unterstützt eine Vielzahl von Paradigmen, einschließlich OOP, funktionaler und prozeduraler Programmierung. PHP ist für die Webentwicklung geeignet, und Python eignet sich für eine Vielzahl von Anwendungen wie Datenanalyse und maschinelles Lernen.

PHP: Datenbanken und serverseitige Logik bearbeiten PHP: Datenbanken und serverseitige Logik bearbeiten Apr 15, 2025 am 12:15 AM

PHP verwendet MySQLI- und PDO-Erweiterungen, um in Datenbankvorgängen und serverseitiger Logikverarbeitung zu interagieren und die serverseitige Logik durch Funktionen wie Sitzungsverwaltung zu verarbeiten. 1) Verwenden Sie MySQLI oder PDO, um eine Verbindung zur Datenbank herzustellen und SQL -Abfragen auszuführen. 2) Behandeln Sie HTTP -Anforderungen und Benutzerstatus über Sitzungsverwaltung und andere Funktionen. 3) Verwenden Sie Transaktionen, um die Atomizität von Datenbankvorgängen sicherzustellen. 4) Verhindern Sie die SQL -Injektion, verwenden Sie Ausnahmebehandlung und Schließen von Verbindungen zum Debuggen. 5) Optimieren Sie die Leistung durch Indexierung und Cache, schreiben Sie hochlesbarer Code und führen Sie die Fehlerbehandlung durch.

Zweck von PHP: Erstellen dynamischer Websites Zweck von PHP: Erstellen dynamischer Websites Apr 15, 2025 am 12:18 AM

PHP wird verwendet, um dynamische Websites zu erstellen. Zu den Kernfunktionen gehören: 1. Dynamische Inhalte generieren und Webseiten in Echtzeit generieren, indem Sie eine Verbindung mit der Datenbank herstellen; 2. Verarbeiten Sie Benutzerinteraktions- und Formulareinreichungen, überprüfen Sie Eingaben und reagieren Sie auf Operationen. 3. Verwalten Sie Sitzungen und Benutzerauthentifizierung, um eine personalisierte Erfahrung zu bieten. 4. Optimieren Sie die Leistung und befolgen Sie die Best Practices, um die Effizienz und Sicherheit der Website zu verbessern.

Wählen Sie zwischen PHP und Python: Ein Leitfaden Wählen Sie zwischen PHP und Python: Ein Leitfaden Apr 18, 2025 am 12:24 AM

PHP eignet sich für Webentwicklung und schnelles Prototyping, und Python eignet sich für Datenwissenschaft und maschinelles Lernen. 1.PHP wird für die dynamische Webentwicklung verwendet, mit einfacher Syntax und für schnelle Entwicklung geeignet. 2. Python hat eine kurze Syntax, ist für mehrere Felder geeignet und ein starkes Bibliotheksökosystem.

PHP und Python: Ein tiefes Eintauchen in ihre Geschichte PHP und Python: Ein tiefes Eintauchen in ihre Geschichte Apr 18, 2025 am 12:25 AM

PHP entstand 1994 und wurde von Rasmuslerdorf entwickelt. Es wurde ursprünglich verwendet, um Website-Besucher zu verfolgen und sich nach und nach zu einer serverseitigen Skriptsprache entwickelt und in der Webentwicklung häufig verwendet. Python wurde Ende der 1980er Jahre von Guidovan Rossum entwickelt und erstmals 1991 veröffentlicht. Es betont die Lesbarkeit und Einfachheit der Code und ist für wissenschaftliche Computer, Datenanalysen und andere Bereiche geeignet.

Warum PHP verwenden? Vorteile und Vorteile erläutert Warum PHP verwenden? Vorteile und Vorteile erläutert Apr 16, 2025 am 12:16 AM

Zu den Kernvorteilen von PHP gehören einfacher Lernen, starke Unterstützung für Webentwicklung, reiche Bibliotheken und Rahmenbedingungen, hohe Leistung und Skalierbarkeit, plattformübergreifende Kompatibilität und Kosteneffizienz. 1) leicht zu erlernen und zu bedienen, geeignet für Anfänger; 2) gute Integration in Webserver und unterstützt mehrere Datenbanken. 3) leistungsstarke Frameworks wie Laravel; 4) hohe Leistung kann durch Optimierung erzielt werden; 5) mehrere Betriebssysteme unterstützen; 6) Open Source, um die Entwicklungskosten zu senken.

Auswirkungen von PHP: Webentwicklung und darüber hinaus Auswirkungen von PHP: Webentwicklung und darüber hinaus Apr 18, 2025 am 12:10 AM

PhPhas significantantyPactedWebDevelopmentAndendendsbeyondit.1) iTpowersMAjorPlatforms-LikewordpressandExcelsInDatabaseInteractions.2) php'SadaptabilityAllowStoscaleForLargeApplicationsfraMe-Linien-Linien-Linien-Linienkripte

PHP vs. Python: Anwendungsfälle und Anwendungen PHP vs. Python: Anwendungsfälle und Anwendungen Apr 17, 2025 am 12:23 AM

PHP eignet sich für Webentwicklungs- und Content -Management -Systeme, und Python eignet sich für Datenwissenschafts-, maschinelles Lernen- und Automatisierungsskripte. 1.PHP hat eine gute Leistung beim Erstellen von schnellen und skalierbaren Websites und Anwendungen und wird üblicherweise in CMS wie WordPress verwendet. 2. Python hat sich in den Bereichen Datenwissenschaft und maschinelles Lernen mit reichen Bibliotheken wie Numpy und TensorFlow übertrifft.

See all articles