Script questions

Student asked about 

I need to find the "virtual directory value relative from the root" of
my website.

Long reply:

‘"virtual directory value relative from the root" of my website. ‘ may have several interpretations, and may have different values and meanings depending upon the context and the server environment.

For example, you could use the PHP Magic Constant

    __FILE__

as in

    $virtual_directory_value = dirname( __FILE__ );

Which, in the case say of this script

    http://www.uvm.edu/~waw/archives/dog.php

has the value of

    /…/uvm.edu/fs/rack1e/u/staff/waw/public_html/archives

Which isn’t so much the "virtual directory value relative from the root" as it is the absolute path of the directory containing the script. And, on www.uvm.edu, at least, a value that may change, since sometimes CIT Technical Resources groups shifts users from one physical disk (rack1e above) to another without warning — so beware if you ever hard-code this value in a script as opposed to obtaining it from the Magic Constant.

In PHP, tried the command line
$HTTP_SERVER_VARS[‘DOCUMENT_ROOT’] but this is not my virtual root, but
something else entirely (it is /racka5/http-data/htdocs). For example,

We have the following other $HTTP_SERVER_VARS

    [SCRIPT_FILENAME] => /users/w/a/waw/public_html/archives/dog.php
    [SCRIPT_URI] => http://www.uvm.edu/~waw/archives/dog.php
    [SCRIPT_URL] => /~waw/archives/dog.php
    [REQUEST_URI] => /~waw/archives/dog.php
    [SCRIPT_NAME] => /~waw/archives/dog.php


$HTTP_SERVER_VARS[SCRIPT_FILENAME] is a preferred alternative to Magic Constant __FILE__, as it returns a fixed alias for the absolute path name of the script. In general, any users alias is formed using the convention

    /users/x/y/xyzzzzz

where x is the first letter of the use’rs NetID, y is the second letter of the user’s NetID, and xyzzzzz is the user’s netid. But again, such conventions are only needed if and when you have to hard-code such a value in a script (usually when you are using some package like older versions of phpBB or wordpress or mediawiki that cat figure it out themselves).

I expect SCRIPT_FILENAME is what you think you are looking for. Then

$spaw_dir = '/spaw/';
$spaw_root = $HTTP_SERVER_VARS['DOCUMENT_ROOT'].$spaw_dir;

Becomes

$spaw_dir = '/spaw/';
$spaw_root = dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME ']).$spaw_dir;

But now let’s put that in the context of the uvmsnow site. If your spaw code lives here

    /users/u/v/uvmsnow/public_html/spaw

and the code you want to have USE spaw is here

    /users/u/v/uvmsnow/public_html/admin

Then the code in, say, admin/demo.php that reads

    $spaw_dir = '/spaw/';
    $spaw_root = dirname($HTTP_SERVER_VARS['SCRIPT_FILENAME ']).$spaw_dir;
    include $spaw_root.'spaw_control.class.php';

tries to include "/users/u/v/uvmsnow/public_html/admin/spaw/"

But that ain’t right — unless you put a copy of /spaw/ in admin. But then ,if you want to use spew from code in another folder, you have to do the same thing. Messy.

So, you could do something funky like this

    $spaw_dir = '/spaw/';
    $spaw_root = $HTTP_SERVER_VARS['SCRIPT_FILENAME'];
    $i=0;
    while (!file_exists($spaw_root.$spaw_dir) && $i < 10) {
        $i++;
        $spaw_root =dirname($spaw_root);
   }
    $spaw_root.=$spaw_dir;
    include $spaw_root.'spaw_control.class.php';

The $i helps to keep the loop in check in case of typos, missing folders, etc.

About Wesley Wright

Born on a mountain top near New York City, Craziest state in the land of the pretty. Raised in the woods so's he knew every tree, Killed him a bear when he was only three.
This entry was posted in Projects, Scripts - Programming. Bookmark the permalink.

Leave a Reply