获取运行脚本的父目录 [英] Get parent directory of running script
问题描述
在PHP中,获取当前运行脚本相对于www根目录的父目录最简单的方法是什么?假设我有:
$ _ SERVER ['SCRIPT_NAME'] =='/relative/path/to/script/index.php '
或者只是:
$ something_else =='/ relative / path / to / script /'
而且我需要使用斜杠正确插入 / relative / path / to /
。你会建议什么一个班轮是首选。
编辑
我需要获得路径相对于www root, dirname(__ FILE __)
给我一个绝对的路径在文件系统,这样将不起作用。 $ _ SERVER ['SCRIPT_NAME']
另一方面'www'起始'。
substr(dirname($ _ SERVER [ 'SCRIPT_NAME']),0,strrpos(dirname($ _ SERVER ['SCRIPT_NAME']),'/')+ 1)
所以如果我有 /path/to/folder/index.php
,这将导致 / path / to / / code>。
In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:
$_SERVER['SCRIPT_NAME'] == '/relative/path/to/script/index.php'
Or just:
$something_else == '/relative/path/to/script/'
And I need to get /relative/path/to/
with slashes properly inserted. What would you suggest? A one liner is preferred.
EDIT
I need to get a path relative to the www root, dirname(__FILE__)
gives me an absolute path in the filesystem so that won't work. $_SERVER['SCRIPT_NAME']
on the other hand 'starts' at the www root.
Got it myself, it's a bit kludgy but it works:
substr(dirname($_SERVER['SCRIPT_NAME']), 0, strrpos(dirname($_SERVER['SCRIPT_NAME']), '/') + 1)
So if I have /path/to/folder/index.php
, this results in /path/to/
.
这篇关于获取运行脚本的父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!