PHP获取当前目录的名称 [英] PHP Get name of current directory

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

问题描述

我的网站上的文件夹中有一个php页面.

我需要将当前目录的名称添加到变量中,例如:

$myVar = current_directory_name;

这可能吗?

解决方案

getcwd();

dirname(__FILE__);

或(PHP5)

basename(__DIR__) 

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

您可以使用basename()来获取路径的结尾部分:)

就您而言,我想说您最有可能希望使用getcwd(),当您的文件需要包含另一个库并且包含在另一个库中时,dirname(__FILE__)更为有用.

例如:

main.php
libs/common.php
libs/images/editor.php

在您的common.php中,您需要使用editor.php中的功能,因此您可以使用

common.php:

require_once dirname(__FILE__) . '/images/editor.php';

main.php:

require_once libs/common.php

这样,当common.php在main.php中为require'd时,在common.php中对require_once的调用将正确地包含在images/editor.php中的editor.php,而不是尝试在当前目录中查找main.php运行.

I have a php page inside a folder on my website.

I need to add the name of the current directory into a variable for example:

$myVar = current_directory_name;

Is this possible?

解决方案

getcwd();

or

dirname(__FILE__);

or (PHP5)

basename(__DIR__) 

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

You can use basename() to get the trailing part of the path :)

In your case, I'd say you are most likely looking to use getcwd(), dirname(__FILE__) is more useful when you have a file that needs to include another library and is included in another library.

Eg:

main.php
libs/common.php
libs/images/editor.php

In your common.php you need to use functions in editor.php, so you use

common.php:

require_once dirname(__FILE__) . '/images/editor.php';

main.php:

require_once libs/common.php

That way when common.php is require'd in main.php, the call of require_once in common.php will correctly includes editor.php in images/editor.php instead of trying to look in current directory where main.php is run.

这篇关于PHP获取当前目录的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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