开发者

server path, minus the current folder

Alright.

Think I am doing this far too complicated and there is an easier solution. I need to get the current server path $_SERVER['DOCUMENT_ROOT']; minus the current folder.

$full_path = $_SERVER['DOCUMENT_ROOT'];
$current_folder = strrchr( $full_path, '/' ); 
$strlength = strlen( $current_folder ) - 1;
$pathlength = strlen( $full_path );
$newlength = $pathlength - $strlength;
$newpath = subst开发者_开发问答r( $full_path, 0, $newlength );

This code works, but I think it might be overkill.

Thanks, Pete


dirname() is very handy for this.
dunno what path you're asking for though, gonna give you both:

$above_root    = dirname($_SERVER['DOCUMENT_ROOT'])."/";
$above_current = dirname(dirname(__FILE__))."/";


Use the function realpath and go one folder higher by adding /../:

$newpath = realpath($_SERVER["DOCUMENT_ROOT"] . "/../");


Couldn't you just do something like this? It's not really pretty, but as far as I know you can just append .. to a path.

$parent =  $_SERVER['DOCUMENT_ROOT'].'/../';

You might want to check if $_SERVER['DOCUMENT_ROOT']; has the directory separator in the end, and if you need to add it or not.


Try:

dirname($_SERVER['DOCUMENT_ROOT']);

PHP offers all kind of functions when working with paths and filesystem: http://www.php.net/manual/en/ref.filesystem.php


$newpath = preg_replace("/[^\/]+$/", "", $_SERVER['DOCUMENT_ROOT']);


Check out this:

$path = $_SERVER['DOCUMENT_ROOT'];
$dirs = explode('\\', $path);
$pathWithoutDir = array_slice($dirs, 0, count($dirs) - 1);

I guess that dirty code aboive will work. You can also change $_SERVER['DOCUMENT_ROOT']; to __DIR__ which is equal to dirname(__FILE__).

EDIT: Code updated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜