« Projects list

Quick links

Relative Path

Once I discovered how to create and use user functions with PHP, I played with it.
Below is a useful function I developped to avoid typping long ./../../../../../ when I wanted to include a file. Of course, now I don't use to use it because I mainly work with frameworks or template engine like Smak.

Script

<?php
/**
 *   ###########
 *   #__________#
 *   __________#
 *   ________#
 *   _____###_____¬≤xiT development
 *   _________#
 *   ___________#
 *   #__________#
 *   _#________#
 *   __#______#
 *   ____####
 *
 *  This function return the relative path from the current dir to root dir
 *  @author Joris Berthelot <admin@eexit.net>
 *  @copyright Copyright (c) 2007, Joris Berthelot
 *  @version 1.01
 *  @param [int $localdir Number of workink dir to access at the local root dir]
 *  @return string $rp Relative path from the current dir (./) to the root dir
 *  @license http://www.opensource.org/licenses/mit-license.php MIT Licence
 */

function __path($localdir 0) {
    
$rp './';
    
$s explode(basename($_SERVER['PHP_SELF']), $_SERVER['PHP_SELF'], 2);
    
$n trim($s[0], '/')
    ? 
count(explode('/'trim($s[0], '/'))) - $localdir 0;
    for (
$i 0$i $n$i++) $rp .= '../';
    return 
$rp;
}
?>

Top

How to use?

The tip is to use it everytime you need to include a file which is in an above directory. Unfortunately, you need to paste this function on every files you want it to be used.

Example with my /foo/bar/baz/foobaz.php, supposing our library is in /foo/lib/, the source code of foobaz.php will start with :

<?php
    
// Sets the tool
    
function __path($localdir 0) { $rp './'$s explode(basename($_SERVER['PHP_SELF']), $_SERVER['PHP_SELF'], 2); $n trim($s[0], '/') ? count(explode('/'trim($s[0], '/'))) - $localdir 0; for ($i 0$i $n$i++) $rp .= '../'; return $rp; }
    
    
// Goes to root directory less one
    
include_once __path(1) . 'lib/myfile.php';
?>

I guess this kind of script is useless now... But, let's getting fun -)

Top

Open Source Licence

Open source

Relative Path uses the MIT Licence.

Copyright © 2007-2018, Joris Berthelot.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Top