mme Posted April 21, 2008 Share Posted April 21, 2008 Hi I would like it so a varible is assigned to the http root <?php $myvar = getenv("DOCUMENT_ROOT"); ?> However I want the last directory to be removed eg; from /var/www/vhosts/mysite.com/httpdocs to /var/www/vhosts/mysite.com Thanks Link to comment https://forums.phpfreaks.com/topic/102239-http-root/ Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Change it in httpd.conf, not in a PHP script. Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523445 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Oh wait. Do you want to use it in the script without the last slash, or do you want to actually change the document root? Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523447 Share on other sites More sharing options...
mme Posted April 22, 2008 Author Share Posted April 22, 2008 I want to use it in a script without the last directory and its slash. In my case without /httpdocs/ Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523452 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 I want to use it in a script without the last directory and its slash. In my case without /httpdocs/ I might be wrong, but try this: $pos = strpos($myvar, 'httpdocs'); $newenv = substr($myvar, 0, $pos); echo $newenv; Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523461 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 If you want it to read "/var/www/vhosts/mysite.com", use: $pos = strpos($myvar, 'httpdocs'); $newenv = substr($myvar, 0, $pos-1); echo $newenv; If you want it to read "/var/www/vhosts/mysite.com/", use: $pos = strpos($myvar, 'httpdocs'); $newenv = substr($myvar, 0, $pos); echo $newenv; Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523463 Share on other sites More sharing options...
mme Posted April 22, 2008 Author Share Posted April 22, 2008 Thanks But what if I wanted to use this on another server that doesnt use httpdocs? without manual changes for each different server? Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523471 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Thanks But what if I wanted to use this on another server that doesnt use httpdocs? without manual changes for each different server? You'd need to code a function that analyzed the string and got the position of the last slash and removed it that way. I could code one I guess, if you really need it. Do you? Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523473 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 function get_new_root() { $oldenv = get_env("DOCUMENT_ROOT"); $slashpos = strrpos($oldenv, "/"); if ($slashpos === false) { return false; } $newenv = substr($oldenv, 0, $slashpos); return $newenv; } Removes everything after the final slash. =) Should work on any server. Usage: $doc_root = get_new_root(); Returns FALSE if it can't find slashes. Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523478 Share on other sites More sharing options...
mme Posted April 22, 2008 Author Share Posted April 22, 2008 Awesome Thanks so much Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523560 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 It worked okay? I hope so. No problem, by the way. Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523562 Share on other sites More sharing options...
trq Posted April 22, 2008 Share Posted April 22, 2008 <?php $myvar = realpath($_SERVER['DOCUMENT_ROOT']) . '/..'); ?> Link to comment https://forums.phpfreaks.com/topic/102239-http-root/#findComment-523676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.