Log in

View Full Version : finding absolute path to current script / without current script



traq
08-30-2009, 09:18 AM
Hi,

I need to figure out how to determine the absolute path to the directory containing the currently executing script:

<?php
$absolute = $_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'];
?>
will get me the path including the file name, but I want it without. Like so:

www.example.com/path/to/
and not like so:

www.example.com/path/to/file.php
If you have the answer, I'd love to hear it :)

Schmoopy
08-30-2009, 10:51 AM
PHP might have something built in for this, but here's something that should work:

You can use one $_SERVER command, instead of two in this instance



<?php

$end = strrpos($_SERVER['SCRIPT_FILENAME'], '/'); // Finds last slash

echo substr($_SERVER['SCRIPT_FILENAME'], 0, $end + 1);

// returns file path with / added on the end, remove + 1 to take this out

?>

traq
08-30-2009, 04:50 PM
hey, cool. I was up late last night looking for a built-in PHP function, but couldn't figure it out. Thanks!