[PHP] calculating psysical path (a first try)

2002-08-01 Thread Lars Olsson

Hi list!

Something I use quite often when programming in ASP is the 
Server.MapPath method. This method expects a virtual path (on the web 
server) and returns the corresponding psysical path. I haven't seen this 
anywhere in PHP, so I've hacked my own little routine to do this (see 
below). But I still have two questions:

a) Is there perhaps a built-in function I can use instead? I browsed 
through the manual quite a lot, but I haven't found quite like this.

b) If there isn't a built-in function, perhaps you could help me make my 
function better? It seems I use a lot of code for this TINY problem...

Here's the function:
snip
// This function calculates the physical path for a given virtual path.
function map_path($virtual_path) {
   // Deklarera necessary variables as global
   global $HTTP_SERVER_VARS;

   // Find out where this file is located in the psysical file system
   $script_filename = $HTTP_SERVER_VARS[SCRIPT_FILENAME];

   // Find out where this file is located in the virtual file system
   $script_name = $HTTP_SERVER_VARS[SCRIPT_NAME];

   // Calculate which folder in the psysical filesystem that corresponds
   // to the root folder in the virtual filesystem
   $psychical_root = substr($script_filename, 0, strlen($script_filename)
 - strlen($script_name));

   // Calculate which folder in the psysical filesystem that corresponds
   // to the given folder in the virtual filesystem
   $psychical_path = $psychical_root . $virtual_path;

   return $psychical_path;
}
/snip

Kindly

/Lasso ([EMAIL PROTECTED])


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] calculating psysical path (a first try)

2002-08-01 Thread Simon Ritchie

I think that you have something like this:

$HTTP_SERVER_VARS['SCRIPT_NAME] == '\foo\bar.php'

$HTTP_SERVER_VARS[SCRIPT_FILENAME] ==
'c:\wwwroot\foo\bar.php'

and you want to use this to derive the root 'c:\wwwroot'.
You can use one of the standard string edit functions. Something like this:

$baseName = ereg_replace(
$HTTP_SERVER_VARS['SCRIPT_NAME' . '$',
'',
$HTTP_SERVER_VARS[SCRIPT_FILENAME]);

The first argument is regular expression.  This one matches the script name
as long as it appears at the end of the string.  The second argument is the
replacement string (nothing), the third is the string to search.

I cover regular expression pattern matching in my introduction to PHP (see
below).

However, the root directory is an Apache variable and these are passed to
PHP in another array.  I can't remember which right now!

Simon Ritchie

Download my introduction to PHP for $25:
http://merrowinternet.com/downloads?source=ml


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php