PHP question

markleeds wrote on Friday, June 10, 2005:

Is there a way that a PHP file during execution can determine it’s own place in the file structure relative to the web root?  I guess this would be the same as knowing it’s URL. 

I thought I had a full proof way of coming up with it, but it turned out to be flawed and overly complicated.

It would save me from hardcoding it into the file.

Basically, I want a PHP page to use Javascript to open another page when a button is pressed.  The open function in JS needs the URL of the PHP page to be opened.  If I hard code it into the PHP file where the JS resides and the file is moved to another directory, it will be broken.

Thanks!

Mark

andres_paglayan wrote on Friday, June 10, 2005:

__FILE__  superconstant is one of those
$PHP_SELF will give it from the www root dir,
$_SERVER["SCRIPT_FILENAME"] will give it from the / dir

markleeds wrote on Friday, June 10, 2005:

__FILE__ produces:
/usr/local/apache2/htdocs/openemr4/interface/forms/flexiform/new.php
which is close to what I need but from below openemr4 (or whatever the webroot will be named)

$_SERVER["SCRIPT_FILENAME"] produces:
/usr/local/apache2/htdocs/openemr4/interface/patient_file/encounter/load_form.php
which is not helpful in my case

$PHP_SELF would have been the most useful, but it is empty for some reason.

Thanks!

sunsetsystems wrote on Friday, June 10, 2005:

Do a phpinfo() call and study the result.

– Rod <rod at sunsetsystems dot com>

andres_paglayan wrote on Friday, June 10, 2005:

Then try $_SERVER["PHP_SELF"]   

markleeds wrote on Saturday, June 11, 2005:

Thanks!  Here is how I get the path I want from what you gave me:

<?php
$myself = __FILE__;
$myself2 = $_SERVER["PHP_SELF"];
$myarray01 = explode("/",$_SERVER["PHP_SELF"]);
$mywebroot = $myarray01[1];
$myarray02 = explode($mywebroot,$myself);
$mywantedpath = $myarray02[1];
echo "<h4>absolute path to this file is: $myself</h4>";
echo "<h4>relative web path to the file that called this file is: $myself2</h4>";
echo "<h4>and webroot is: $mywebroot</h4>";
echo "<h4>and wanted path is: $mywantedpath</h4>";
?>

The output is:

absolute path to this file is: /usr/local/apache2/htdocs/openemr4/interface/forms/flexiform/new.php
relative web path to the file that called this file is: /openemr4/interface/patient_file/encounter/load_form.php
and webroot is: openemr4
and wanted path is: /interface/forms/flexiform/new.php

I want to be able to call php files from a my new.php form page with Javascript.  The pages I call up will be in the same form directory.  It is complicated by the fact that the new.php file is not called up directly but is included in another file - load_form.php - first.