获取运行脚本的父目录 [英] Get parent directory of running script

查看:98
本文介绍了获取运行脚本的父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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'起始'。

解决方案得到它自己,这有点kludgy,但它的工作原理:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆