Shoutbox

get URI folder for executing PHP script - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: get URI folder for executing PHP script (/showthread.php?tid=77080)

get URI folder for executing PHP script by MeEtc on 08-28-2007 at 11:49 PM

basically, I am looking for a method to obtain the directory where the current script is running. I do know that $_SERVER['REQUEST_URI'] contains the full url, but it is not cross platform. (*cough* IIS *cough*)

so for any url, http://subdomain.domain.tld/folder/folder2/file.php, i need http://subdomain.domain.tld/folder/folder2/ to be returned

help please? anyone?


RE: get URI folder for executing PHP script by WDZ on 08-29-2007 at 12:27 AM

quote:
Originally posted by MeEtc
I do know that $_SERVER['REQUEST_URI'] contains the full url, but it is not cross platform. (*cough* IIS *cough*)
Code stolen from WordPress... :tongue:
code:
// Fix for IIS, which doesn't set REQUEST_URI
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; // Does this work under CGI?

    // Append the query string if it exists and isn't null
    if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
        $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
    }
}
REQUEST_URI won't contain the hostname though... you can get that from HTTP_HOST.
RE: get URI folder for executing PHP script by MeEtc on 08-29-2007 at 12:37 AM

oops, did i say REQUEST_URI? i meant SCRIPT_URI...
Thanks DZ, but that still doesn't take out the file name...


RE: get URI folder for executing PHP script by roflmao456 on 08-29-2007 at 12:40 AM

stolen from php.net :cheesy:

code:
<?php

$path_only = implode("/", (explode('/', $_SERVER["SCRIPT_URI"], -1)));

print $path_only;

?>


RE: get URI folder for executing PHP script by WDZ on 08-29-2007 at 12:58 AM

SCRIPT_URI? I've never seen that used in PHP... :-/ (Edit: Apparently it only exists under Apache with mod_rewrite enabled)

Anyway, the easiest way to remove the filename is to use the dirname() function... :p

code:
if(empty($_SERVER['REQUEST_URI'])) {
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
}

// Strip off query string so dirname() doesn't get confused
$url = preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI']);
$url = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';
http://shoutbox.menthix.net/crap/asdf/uri_test.php :spam:
RE: get URI folder for executing PHP script by MeEtc on 08-29-2007 at 02:41 AM

code:
echo(implode("/", (explode('/', $_SERVER["SCRIPT_URI"], -1))).'/');
thanks rofl
RE: get URI folder for executing PHP script by roflmao456 on 08-29-2007 at 03:27 AM

quote:
Originally posted by WDZ
http://shoutbox.menthix.net/crap/asdf/uri_test.php :spam:
[offtopic/]
lol @ /crap/asdf