How to get script file path?

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
I have a php script file that I include in other main web page.

I would need the script file to be able automatically check its location.
If using $_SYSTEM variable I get the main files path and not the script file path.

How to get script file path?
 
You have to give the full path to your script.
You can see your server path using php's phpinfo() function.

You can set your included path using php's set_include_path() function or via .htaccess if your using Apache (example):
Code:
# Apache command
# PHP INCLUDE PATH - WINDOWS ; UNIX :

php_value include_path /home/..../public_html/include/:/home/.../public_html/

Also try checking what you have using $_SERVER['PATH_TRANSLATED' ].

Regards
 
Thank you for quick reply.

1. phpinfo() has been disabled for security reasons.
2. $_SERVER['PATH_TRANSLATED' ] give the path to main file not the script for me.
3. Rather not use .htaccess as it is OS dependent


4. set_include_path() not quite clear to me yet, but maybe could be solution. I will check it out today when I get some time over.

I hoped the server variables would have solved the problem but seems not. These are the ones I tried.
print $_SERVER['PHP_SELF'];
print $_SERVER['SCRIPT_FILENAME'];
print $_SERVER['PATH_TRANSLATED'];
print $_SERVER['SCRIPT_NAME'];
print $_SERVER['REQUEST_URI'];
 
If you can't know what's the server path to your folder/files, ask your hoster.
After that, you can define some constant so that you'll easily call to your scripts.
Example:
Code:
<?php

define('MyPath', '/home/..../public_html/include/');

// ... 

require_once(MyPath . 'common.php');

?>
 
Sorry, maybe I did not describe my problem correctly.
I do kow the server path.

My problem is following:

My file locations
/myfiles/mainfile.php
/inc/functions.php'

mainfile.php import myscript.php with by require_once('../inc/functions.php');

functions.php script wants to know the location of it self.
using print $_SERVER['PATH_TRANSLATED']; in functions.php script returns "/myfiles/mainfile.php" instead of /inc/functions.php
cause it is imported to mainfile.php
.
 
Alexei Kubarev said:
$_SERVER['PATH_TRANSLATED'] ?

try checking $_SERVER superglobal variables

Well not quite understand.
I tried $_SERVER variablessas described above.

Is superglobal something different thing?
 
well..superglobals are:

  • $_GET
    $_POST
    $_SERVER
    $_SESSION
    $_COOKIE
    $_FILES
    $_REQUEST
    $_ENV
    $GLOBALS

$_SERVER has lots of varialbes that you may use: $_SERVER['PHP_SELF'] is one of them, $_SERVER['PATH'] and $_SERVER['PATH_TRANSLATED'] are others..there are some more that you may want to use... check http://www.php.net/en/manual for more details
 
Try this out to see what you get:

$_SERVER["DOCUMENT_ROOT"];
$_SERVER["SITE_ROOT"];
$_SERVER["SITE_HTMLROOT"];
 
Riku Tuominen said:
Sorry, maybe I did not describe my problem correctly.
I do kow the server path.

My problem is following:

My file locations
/myfiles/mainfile.php
/inc/functions.php'

mainfile.php import myscript.php with by require_once('../inc/functions.php');

functions.php script wants to know the location of it self.
using print $_SERVER['PATH_TRANSLATED']; in functions.php script returns "/myfiles/mainfile.php" instead of /inc/functions.php
cause it is imported to mainfile.php
.

edit: *
Alexei Kubarev
I di try print $_SERVER['PHP_SELF'];
print $_SERVER['SCRIPT_FILENAME'];
print $_SERVER['PATH_TRANSLATED'];
print $_SERVER['SCRIPT_NAME'];
print $_SERVER['REQUEST_URI'];
*
gesf
seems there still is confusion what I am trying to do.
gesf i do not need the server path.
edit. meaning I do know it.
The problem is described above.

*edit
Propably cause of my bad english I missunderstod and gesf you understud me.
*
but gesf i think you solved the problem with:
define('MyPath', '/home/..../public_html/include/');

How long will that 'MyPath' be valid?

Thaks to you all for helping me. :)
 
Alexei Kubarev said:
Defined variables are valid until the script is terminated..

Thanks. I will test it out tomorrow.

Thanks to you both. :)
 
Back
Top