<?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>All About Web &#38; Mobile Application Development &#187; Absolute Path</title>
	<atom:link href="http://www.londatiga.net/tag/absolute-path/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.londatiga.net</link>
	<description>All About Web &#38; Mobile Application Development</description>
	<lastBuildDate>Mon, 30 Aug 2010 17:26:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How To Define Global Absolute Path in PHP</title>
		<link>http://www.londatiga.net/it/how-to-define-global-absolute-path-in-php/</link>
		<comments>http://www.londatiga.net/it/how-to-define-global-absolute-path-in-php/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 09:14:27 +0000</pubDate>
		<dc:creator>lorenz</dc:creator>
				<category><![CDATA[Featured Articles]]></category>
		<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Absolute Path]]></category>
		<category><![CDATA[Relative path]]></category>

		<guid isPermaLink="false">http://www.londatiga.net/?p=75</guid>
		<description><![CDATA[A path is general form of a filename or directory that specified a unique location in a file system. In web programming, a path  is used to define links (url) to documents and images or to include a specified library file. There are two standard ways to define a path:

Absolute Path, is a path that [...]]]></description>
			<content:encoded><![CDATA[<p>A path is general form of a filename or directory that specified a unique location in a file system. In web programming, a path  is used to define links (url) to documents and images or to include a specified library file. There are two standard ways to define a path:</p>
<ol>
<li><strong>Absolute Path</strong>, is a path that points to the same location on file system regardless of the working directory or combined paths and usually written in reference to root directory.</li>
<li><strong>Relative Path</strong>, is a path  that relative to working directory.</li>
</ol>
<p>In many ways, absolute path is much usefull because it doesn&#8217;t need us to redefine the path or link if we change the working directory. For an example, below is a directory hierarchy of a web application that located on a development server  that can be accessd via url <span style="color: #0000ff;">www.mysite.com</span>, where <span style="color: #0000ff;">/var/www/html </span>is it&#8217;s web server&#8217;s document root and application root directory and &#8216;<span style="color: #0000ff;">/&#8217; <span style="color: #000000;">is the application root url</span></span>.</p>
<div class="wp-caption alignnone" style="width: 260px"><img title="Document Root" src="http://londatiga.net/images/docroot.jpg" alt="Document Root" width="250" height="181" /><p class="wp-caption-text">Document Root</p></div>
<p><span style="color: #0000ff;"><span style="color: #000000;">Take a look at<span style="color: #008000;"> index.php</span> in <span style="color: #0000ff;">profile</span> directory, to define an url to<span style="color: #008000;"> <span style="color: #339966;">mypic.jpg</span></span> in <span style="color: #0000ff;">images</span> directory and include <span style="color: #339966;">lib.php</span> from <span style="color: #0000ff;">lib</span> directory:<br />
</span></span></p>
<p><span style="color: #0000ff;"><span style="color: #000000;">using absolute path:<br />
</span></span></p>
<pre class="brush: php;">
&lt;?php
include '/var/www/html/lib/lib.php';

$str = '&lt;img src=&quot;/images/mypic.jpg&quot;&gt;';
?&gt;
</pre>
<p><span style="color: #0000ff;"><span style="color: #000000;">using relative path:<br />
</span></span></p>
<pre class="brush: php;">
&lt;?php
include '../../lib/lib.php';

$str = '&lt;img src=&quot;../../images/mypic.jpg&quot;&gt;';
?&gt;
</pre>
<p><span style="color: #0000ff;"><span style="color: #000000;">Using absolute path, it wouldn&#8217;t be a matter where the working directory is, but it would be a problem if  you change the application root directory into  another directory other than web server&#8217;s document root, for example in <span style="color: #0000ff;">/var/www/html/</span><span style="color: #0000ff;">web</span>, so your application would be accesed via <span style="color: #0000ff;">www.mysite.com/web</span>. We have to edit manually each file that using absolute path because the root directory will changed to <span style="color: #0000ff;">/var/www/html/</span><span style="color: #0000ff;">web</span> so the included file will not work.</span></span></p>
<p><span style="color: #0000ff;"><span style="color: #000000;">To overcome this problem, you have to define a global variable that specified absolute path that generally applied to all directories in your application. The  solution is to define two variables which will be used to define root directory for file operations and root url for url operations  in a file that would be placed in the root directory .</span></span></p>
<pre class="brush: php;">
&lt;?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?&gt;
</pre>
<p>Save it as init.php and place it in your application root directory.So, from our example above, using the two variables:</p>
<pre class="brush: php;">
&lt;?php
include '../../init.php';
include ROOT_DIR .  '/lib/lib.php';

$img = '&lt;img src=&quot;' .  ROOT_URL . '/images/my.pic.jpg&quot;&gt;';
?&gt;
</pre>
<p>As you can see, it will be applied in any directories regardless where the current working directory or server document root.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.londatiga.net/it/how-to-define-global-absolute-path-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
