[PHP] File Roots & Path Functions
These two functions are great for CMS engines that work with files & folders + data in a database.
This first one calculates the path back to the root with a given file and root. It returns '../../../' depending on the depth of the tree.
Version 1
PHP Code:
function pathToRoot($root,$file)
{
/* Function: pathToRoot() created by `s-p-n` For: `WysGui.com`
pathToRoot calculates and returns the path from a specified file back to the specified root.
Example:
pathToRoot($_SERVER['HTTP_HOST'],$_SERVER['REQUEST_URI'])
(returns the path from a remote file back to the host)
*/
$r = count(explode('/',$root));$f = count(explode('/',$file));
for($i=1;$i<($f-$r);$i++)
{
$str .= '../';
}
return $str;
}
I personally use this function on every page in the header.php file.
I call it $_root because it helps me coordinate through HTML like I do in flash.
So I call this var in header.php:
PHP Code:
$_root = pathToRoot($_SERVER['HTTP_HOST'],$_SERVER['REQUEST_URI']);
And on my Nav bar all of the links are $_root.$url. If there is no root, no problem. When a root comes up, again, no problem. (as long as the nav links are relative, of course ;) :ott1:
--------------------------------------------------------------
This second function is a fix for some glitches in $_SERVER['REQUEST_URI'] that shows query strings, seizes to show the index as default when the user is visiting a folder. This function returns "path/to/file.ext".
Expanded Version:
PHP Code:
function wysgui_uri($index)
{
/* Function: wysgui_uri() created by `s-p-n` For: `WysGui.com`
$index is optional, default is 'index.php'
Finds this URL and strips all the crap out.
Opens door for smooth coordination between files & MySQL data
If no page is found, it must be root/folder/index.php
(wysgui.com -> index.php, wysgui.com/folder -> folder/index.php)
Example, might be blank for 'wysgui.com/', but we know it's index.php.
*/
$cleanURL = preg_replace('/[?]|'.$_SERVER['QUERY_STRING'].'/','',substr($_SERVER['REQUEST_URI'],1));
$cond = strspn('.',$cleanURL);
$t = $cleanURL;
$f = strlen($index)>0?$index:'index.php';
if(!$cond)
{
$t.=$f;
}
return $t;
}
Collapsed Version:
PHP Code:
function wysgui_uri($index)
{
/* Function: wysgui_uri() created by `s-p-n` For: `WysGui.com`
$index is optional, default is 'index.php'
Finds this URL and strips all the crap out.
Opens door for smooth coordination between files & MySQL data
If no page is found, it must be root/folder/index.php
(wysgui.com -> index.php, wysgui.com/folder -> folder/index.php)
Purpose: $_SERVER['REQUEST_URI'] might be blank for 'wysgui.com/', but we know it's index.php.
It also might contain query strings and junk we don't want.
*/
$cleanURL = preg_replace('/[?]|'.$_SERVER['QUERY_STRING'].'/','',substr($_SERVER['REQUEST_URI'],1));
return strspn('.',$cleanURL)?$cleanURL:$cleanURL.(strlen($index)>0?$index:'index.php');
}
I hope these functions are of use to people ;)
The point for both these functions is destroyed when you decide not to use relative paths. However, if you are going to make a CMS that stores all of the pages with relative paths, (which allows the whole site to be moved from folder-to-folder or across domains), you can do as I do using this system originally developed for http://www.wysgui.com. Nav links are never messed up, and the pageData in the database will have stored the relative page paths; [ which links up exactly with wysgui_uri() ].
If there's a function like either of these default with PHP that'd be cool but I haven't seen one yet, so enjoy. :drool:
Re: [PHP] File Roots & Path Functions
Nice one. Back in my PHP days I always used mod_rewrite, which caused wrong paths to files, this was my way to prevent it:
PHP Code:
// Get the number of '../' to be placed in front of a redirection. This is to prevent page mismatching that comes with mod_rewrite
function get_relative_dir($actual_dir, $request_dir)
{
if ($actual_dir{strlen($actual_dir)-1} == '/')
{
$actual_dir = substr($actual_dir, 0, -1);
}
elseif (!strlen($actual_dir))
{
$actual_dir = '.';
}
if ($request_dir{strlen($request_dir)-1} != '/')
{
$request_dir = dirname($request_dir);
}
elseif($actual_dir.'/' == $request_dir)
{
return './';
}
$relative_path = '';
while ($actual_dir != $request_dir && strlen($request_dir) > strlen($actual_dir))
{
$request_dir = dirname($request_dir);
$relative_path .= '../';
}
if (!$relative_path)
{
return './';
}
return $relative_path;
}
// Define the directories
define('RELATIVE_DIR', get_relative_dir(dirname($_SERVER['PHP_SELF']), urldecode($_SERVER['REQUEST_URI'])));
Now, in front of any link I would put this constant variable:
PHP Code:
<link rel="stylesheet" href="<?php echo RELATIVE_DIR; ?>style.css" type="text/css">
<a href="<?php echo RELATIVE_DIR.'link/to/page'; ?>">Link to page</a>