Hey,
I have written a function to grab the system path of the scripts home directory.
Often, the functions file is used from a directory deeper inside the script home directory, but we still want to return the main script directory.
Example: Script is accessed from /home/public_html/ and then accessed from /home/public_html/install/ where we still want to return /home/public_html/ as that is the main script home directory.
The function used is below, as you will notice i use strrpos() to return the position of the exclusion although supposedly, strings will not work below PHP 5. The server we tested on has PHP 4.4 installed with cPanel.
I tested this before i relised strings were not supported and tested with 2 exclusions which returned different results.
get_real_path("/install/"); would return /home/public_html/install while get_real_path("install/"); would return /home/public_html (as it should)
As strings were not included below PHP 5, i am wondering why the second return should differ from the first or why it would even work at all.
using get_real_path("install/"); is no problem, but i do not want to use it if it may cause problems on specific PHP installations.
Could someone possibly shed some light on the situation or an alternative to my function?
I have written a function to grab the system path of the scripts home directory.
Often, the functions file is used from a directory deeper inside the script home directory, but we still want to return the main script directory.
Example: Script is accessed from /home/public_html/ and then accessed from /home/public_html/install/ where we still want to return /home/public_html/ as that is the main script home directory.
The function used is below, as you will notice i use strrpos() to return the position of the exclusion although supposedly, strings will not work below PHP 5. The server we tested on has PHP 4.4 installed with cPanel.
I tested this before i relised strings were not supported and tested with 2 exclusions which returned different results.
get_real_path("/install/"); would return /home/public_html/install while get_real_path("install/"); would return /home/public_html (as it should)
As strings were not included below PHP 5, i am wondering why the second return should differ from the first or why it would even work at all.
using get_real_path("install/"); is no problem, but i do not want to use it if it may cause problems on specific PHP installations.
Could someone possibly shed some light on the situation or an alternative to my function?
PHP Code:
function get_real_path($exclusion=false)
{
$cwd = getcwd();
$cwd = str_replace( "\\", "/", $cwd );
if ($exclusion !== false)
{
// Get position of the exclusion
$stop = strrpos($cwd, $exclusion);
if ($stop === false)
{ $stop = strlen($cwd); }
$real_path = substr( $cwd, 0, $stop );
}else{
$real_path = $cwd;
}
// Remove last forward slash
$check_last = @strrpos($real_path, '/');
if ( $check_last+1 == @strlen($real_path) )
{
$real_path = substr($real_path, 0, $check_last);
}
return $real_path;
}
Comment