Brinkster Knowledge Base

KB Home   /  Support  /  Code Snippets  /   PHP Realpath example
PHP Realpath example Articles
Copy and paste this code snippet into your .php page, then view the page in your web browser.
http://yoursite.com/page.php
We suggest using Realpath instead of using an absolute path in your coded applications.

*Note that no edit to the code needs to be made at all to run this demonstration.*

<html>
<head><title>PHP Realpath Demo</title></head>
<body>
<?php
echo "<font face='HELVETICA'><b>PHP Realpath Demonstration</b><br><br>";
echo "The &quot;File.txt&quot; file does not actually exist;unless you created one that is, this is used as a demonstration of how to use realpath.<br>";
echo "For a more dramatic effect, place this file in a sub folder of your website's root folder.<br><br>";
echo "This path will take you to the folder this file is currently running in.<br>";
echo "<table border=0><tr><td><b>PHP Path: &nbsp;&nbsp;&nbsp;&nbsp;</b></td><td>realpath&#40;&#39;&#39;&#41;.&#39;&#92;file.txt&#39;;</td></tr>";
echo "<tr><td><b>Output:</b></td><td>".realpath('').'\file.txt'."</td></tr></table>";
echo "<br><br>";
echo "This path will take you to the root of your website.<br>";
echo "<table border=0><tr><td><b>PHP Path: &nbsp;&nbsp;&nbsp;&nbsp;</b></td><td>realpath&#40;&#36;_SERVER&#91;&#39;DOCUMENT_ROOT&#39;&#93;&#41;.&#39;&#92;file.txt&#39;;</td></tr>";
echo "<tr><td><b>Output:</b></td><td>".realpath($_SERVER['DOCUMENT_ROOT'])."\file.txt</td></tr></table>";
echo "<br><br>For a more detailed explanation of Realpath please visit this <a style=text-decoration:none href='http://www.w3schools.com/php/func_filesystem_realpath.asp'>Helpful originalAttribute=href originalPath='http://www.w3schools.com/php/func_filesystem_realpath.asp'>Helpful originalAttribute=hreforiginalPath='http://www.w3schools.com/php/func_filesystem_realpath.asp'>Helpful Site</a>";
?>
</body>
</html>