Forum Moderators: coopster

Message Too Old, No Replies

How to get the path of the current script, if it's an include

         

scubby

7:49 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



How do you get the path of the currently executing script if the script is a php include?

So in index.php, I have:

<?php include("/the/directory/file.php");?>

Then in file.php, I would need to get:

/the/directory/

PHP_SELF seems to return the location of the parent file.

d40sithui

9:04 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



i think __FILE__ might work.
<?
echo __FILE__;
?>

scubby

11:14 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



No, that seems to contain the path to the file on disk, as opposed to the web path.

PHP_Chimp

8:38 am on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are getting a full file path i.e.
var/domains/www/htdocs/example.com/..
Then you could always just use str_replace [uk3.php.net]to remove all of the ../htdocs/ and replace that with $_SERVER['HTTP_HOST'].

Im not sure how this would cope if you have aliases in there, although I think it should work.

php4U

2:23 am on Dec 6, 2007 (gmt 0)

10+ Year Member



You may have already solved this problem, but this might help you. d40sithui was on the right track.

Try
<?php echo dirname(__FILE__);?>

In the future you could always just put this in a file named path.php...and upload it to the directory you want to get the path to. You can then copy/paste it into the script you are using that needs the server directory path.

scubby

3:10 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



Thanks guys, got it working.