<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程默的博客 &#187; 相对路径</title>
	<atom:link href="http://blog.chacuo.net/tag/%e7%9b%b8%e5%af%b9%e8%b7%af%e5%be%84/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chacuo.net</link>
	<description>web原理、web架构、web安全、web性能、服务器性能、服务器架构、服务器安全;你不能预知明天，但你可以利用今天。你不能样样顺利，但你可以事事尽力!</description>
	<lastBuildDate>Mon, 31 Aug 2020 15:33:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>PHP 站点相对包含，路径的问题解决方法（include,require)</title>
		<link>http://blog.chacuo.net/134.html</link>
		<comments>http://blog.chacuo.net/134.html#comments</comments>
		<pubDate>Sat, 01 Jun 2013 09:53:18 +0000</pubDate>
		<dc:creator>程默</dc:creator>
				<category><![CDATA[web原理]]></category>
		<category><![CDATA[web性能]]></category>
		<category><![CDATA[学习心得]]></category>
		<category><![CDATA[相对路径]]></category>

		<guid isPermaLink="false">http://blog.chacuo.net/?p=134</guid>
		<description><![CDATA[以前看了，很多框架，基本上很少使用相对路径包含。而一般很多做php web站点，喜欢用相对路径。 认为这样，无 [...]]]></description>
				<content:encoded><![CDATA[<p>以前看了，很多框架，基本上很少使用相对路径包含。而一般很多做php web站点，喜欢用相对路径。 认为这样，无论目录放到那里。 只要跟另外目录关系一致。那么就不会出现问题。如果一个站点，一般都认为，如果用根目录，经常会改变网站地址，觉得很不方便。其实，我们从各大常见框架里面会发现，基本上都是采用是绝对路径方法。</p>
<ul>
<li>
<h3><font style="font-weight: bold">相对路径带来问题</font></h3>
</li>
</ul>
<p>我们有如下结构的目录。</p>
<blockquote><pre style="text-indent: 0px">&lt;web&gt;(网站根目录)
├&lt;a&gt;文件夹
│ │
│ └a.php
├&lt;b&gt;文件夹
│ │
│ └b.php
└test.php</pre>
</blockquote>
<p></p>
<p>&#160;</p>
<p>如果b.php 包含a.php (include(“../a/a.php”)) ，然后test.php 包含b.php (include(“b/b.php”)) ，我们发现很奇怪问题。</p>
<p>首先访问：b.php 可以正常访问， 然后访问test.php</p>
<p><a href="http://blog.chacuo.net/wp-content/uploads/2013/06/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blog.chacuo.net/wp-content/uploads/2013/06/image_thumb.png" width="769" height="103" /></a></p>
<p>发现，找不到 a.php了。 这里注意一个问题是：<strong><u>php 默认相对路径都是以，被访问页面所在路径为准的。无论一个入口页面，里面包含多少文件，相对路径，都是以这个页面为准</u></strong>。</p>
<p>如果访问test.php 根路径是：test.php，如果访问b.php 相对路径都以b.php 所在路径为准。刚刚页面test.php 包含了b.php,b.php包含了a.php. 所有包含都以test.php 为准的。</p>
<p>估计刚刚开始php学习朋友，经常遇到这个问题，而且发现经常出现一大堆警告影响大家学习的兴趣。</p>
<ul>
<li>
<h3><font style="font-weight: bold">使用绝对路径方法</font></h3>
</li>
</ul>
<p>各大开源框架基本上采用绝对路径方法，这样可以避免相对路径因为包含访问文件变了。基准路径变化，让包含出现错误了。 所以，我们看看常见方法。</p>
<p>首先将网站基准订到一个固定文件。一般可以用下面方法实现。如：根目录下面有个config.php文件。</p>
<blockquote>
<p><font style="background-color: #ffffff" face="Arial">&lt;?php </font></p>
<p><font style="background-color: #ffffff" face="Arial">define(‘Root_Path’,<b>dirname(__FILE__));</b></font></p>
</blockquote>
<p>__FILE__ 至的是当前脚本路径，在那个脚步php里面调用该变量，它的值就是该脚步的绝对路径。</p>
<p>然后，任何其它页面，在做包含时候，只需要包含了该config.php后。</p>
<blockquote>
<p><font style="background-color: #ffffff" face="Arial">&lt;?php</font></p>
<p><font style="background-color: #ffffff" face="Arial">包含config.php…..</font></p>
<p><font style="background-color: #ffffff" face="Arial">include(Root_Path.”/文件路径”);即可</font></p>
</blockquote>
<p>&#160;</p>
<h3></h3>
<ul>
<li>
<h3><strong>使用绝对路径好处</strong></h3>
</li>
</ul>
<p>使用解决路径好处除了可以在大型项目中，包含时候更准确定位到文件，不易产生错误外。还有另外一个好处，包含文件，性能会得到很大提升。</p>
<p>如果给一个相对位置包含，php查找该文件，一般会在set_include_path 函数，设置的所有路径里面去搜索。 我们知道，要一个一个去尝试，列举目录，然后查找文件。这直接会消耗大的IO。 也会消耗很多性能。 如果我们用绝对包含，直接就可以准确判断出，文件是否存在。不会去set_include_path设置目录去查找了。 </p>
<p>以上问题，对于刚刚接触到php大型项目开发，可能会很容易遇到。欢迎讨论！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chacuo.net/134.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
