Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
help me analyse this function
Old 05-04-2009, 11:26 AM help me analyse this function
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I created this function/procedure, which extracts a relative path from the absolute path the to server (to be used in front end code, like specifying the path to an image). The reason I created this, is I want my content management system to be ultimately portable. I want to avoid using absolute URI paths, starting with http://, because sites are sometimes developed under different domain names.

This means I want my path to go from something like: "/home/www/user/sitename/directoryname/" to "/directoryname/" where /directoryname/ is the first directory in the root of the site itself.

My function works, but I wonder if it will be perfect in all situations. I think there will be some instances where it may not work, but I'm not sure. I just patched it for instances when the last directory in the DOCUMENT_ROOT will occur in the upper path.

Worth noting, is that on some server configurations, the DOCUMENT_ROOT does not match the filepath obtained with dirname(__FILE__). This presents an interesting problem and is why I can't just replace the first instance of DOCUMENT_ROOT in comparison to the dirname() of the config file to obtain the path. However, even when the DOCUMENT_ROOT is not the same as the dirname() of root directory, I've found that the last couple of directory names are always the same.

I'm trying to make this configuration ultimately portable, so that it can be placed literally anywhere on the web, on any server, or even any directory within a site, and automatically "know" where it is.

Anyway, here it is, if anyone is bored and wants to take a look:

PHP Code:
define ("ROOT"dirname(dirname(__FILE__)));
//define("BASE", "/root/directory")

if(!defined("BASE")) {
    function 
base($path) {
        
$base "";//default
        
$compare $_SERVER["DOCUMENT_ROOT"];
        if(
substr($compare, -1) == "/") {
            
$compare substr_replace($compare"", -1); //because some servers have a / at the end and some don't
        
}
        while(
basename($path) != substr($comparestrlen(basename($path)))) {
            
//if basename exists at end of DOCUMENT_ROOT, doesn't continue
            
$oldpath $path;
            
$anchor "/".basename($path);
            
$base $anchor.$base;
            
$path dirname($path);
        }

        if(!empty(
$base) && substr_count($base$anchor) !== substr_count($oldpath$anchor) - substr_count($compare$anchor))
        {
//recursive if needed (because last directory of DOCUMENT_ROOT might exist in the URI path)
            
$base base($path);
        }
        return 
$base;
    }
    
    
define ("BASE"base(ROOT));

__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 05-04-2009 at 11:49 AM.. Reason: removed debugging line
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
 
Register now for full access!
Reply     « Reply to help me analyse this function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14762 seconds with 12 queries