dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: need help with syntax for finding the relative path to the root of a website w/php

  1. #1
    Senior Member
    Join Date
    Sep 2000
    Location
    San Francisco
    Posts
    196

    need help with syntax for finding the relative path to the root of a website w/php

    Hi,

    is parsing a string the best way to find the relative path to the root of a website?
    If i use this "finPath()" function in all my <img src> tags will it bog down the server?

    I wanted to be able to use a template php page and have it be able find the images and scripts regardless of its location on the server

    I have a php variable on a page

    $SITE_ROOT = "http://www.mydomain.com"

    I need to write a function that calculates the relative path to the $SITE_ROOT no matter where you are in the site. For example
    if i am in "http://www.mydomain.com/dir1/dir2/anotherDir/index.php" I want to be able to call my getPath() function and have it return a string containing "../../../"

    here is my logic, i need a little help converting it to the proper syntax, and if you find any flaws in the logic or know a better or more efficient way please let me know:

    PHP PSEUDOCODE:
    fullpath = realpath('.');
    pathDepth = substring(fullpath, lengthOfSITE_ROOT, lengthOf_fullpath);
    parse pathDepth using delimeter "/"
    for (number of "/" in pathDepth)
    path .= "../";
    return path;

    I really appreciate anyones and everyones input on this.

    ~Dev

  2. #2
    Senior Member
    Join Date
    Sep 2000
    Location
    San Francisco
    Posts
    196

    FYI

    It didnt look like I was going to get any help on this one so i wrote the function myself. I'm not sure how efficient it is but at least I works. I'm still wondering how many times I could call this script on a single page withoout bogging down the server with a massive amount of string manipulations:
    PHP Code:
        function getRelativePath($siteRoot$dest) {
            
    $path = (string) $path;
            
    $count = (int) $count;
            
    $fullPath substr(realpath('.'), strlen($siteRoot), strlen(realpath('.')));
            
    $dirArr explode("/"$fullPath);
            if(
    sizeof($dirArr) > 1) {
                foreach (
    $dirArr as $dir) {
                    
    $dirCount++;
                    if(
    $dirCount 1) {
                        
    $path .= "../";
                    }
                }
                
    $path .= $dest;
            } else {
                
    $path $dest;
            }
            return 
    $path;
    }
    echo 
    getRelativePath('/home/virtual/mydomain.com/var/www/html/''scripts/myScript.php'); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center