Saturday 17 August 2013

PHP path checking script

Finding PHP paths can be difficult on web servers.  Depending on how they are setup the paths may vary from host to host.  You may have or have not permissions to change the php.info file to edit configurations as you need.

Here is a script to quickly check how the server paths are setup.

<?php
//php path checker  checkPath.php

echo 'PHP_SELF is: '.$_SERVER['PHP_SELF'].'<br>';
echo 'DOCUMENT_ROOT is: '.$_SERVER['DOCUMENT_ROOT'].'<br>';
echo 'dirname(__FILE__) is: '. dirname(__FILE__).'<br>';
echo 'basename(__FILE__) is: '.basename(__FILE__).'<br>';
echo 'basename(dirname(__FILE__)) is: '.basename(dirname(__FILE__)).'<br>';
?>

expected output


PHP_SELF is: /scripts/checkPath.php
DOCUMENT_ROOT is: /usr/local/apache/htdocs
dirname(__FILE__) is: /home/userName/public_html/scripts
basename(__FILE__) is: checkPath.php
basename(dirname(__FILE__)) is: /scripts


Tips for portability of PHP scripts.  Sometimes the best way to specify php paths in includes for your scripts is to create a configuration.php file.
In this file, assign variables to the full path for your scripts there.

<?php
//configuration.php
$base = "/home/userName/public_html/scripts";
$header = "/header.php";
$footer = "/footer.php"
?>

Then use these lines in you scripts for includes in the following fashion:

<?php
//myScript.php
include "/home/userName/public_html/scripts/configuration.php";
include "$base.$header";
echo "some text";
include "$base.$footer";
?>

You still have to use a full path link for the configuration file once, but that is it.

WindyCityTech Blogger
WindyWindyCityTech Wordpress

No comments:

Post a Comment