Jump to content

HTTP ROOT


mme

Recommended Posts

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
Share on other sites

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
Share on other sites

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? :o

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.