我的网站上有一个文件夹,里面有一个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()来获取路径的尾部:)

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

如:

main.php
libs/common.php
libs/images/edit或.php

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

common.php:

require_once dirname(__FILE__) . '/images/edit或.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 c或rectly includes edit或.php in images/edit或.php instead of trying to look in current direct或y where main.php is run.

Php相关问答推荐

使用额外的输入字段自定义WooCommerce单个产品

如何在WordPress中为特定自定义帖子类型自定义URL struct

累加平面数组中数字子集的所有组合,以生成组合及其乘积的二维数组

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

Msgraph-sdk-php v2如何从返回对象中获取数据?

Laveel Livewire Get Groupby姓名列表

WooCommerce我的帐户:从帖子作者处获取自定义帖子类型帖子ID

只收取WooCommerce中最高的运费

针对给定产品类别的WooCommerce计数审核

shell_exec运行大型数据处理

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

将 PHP 版本更改为 8.1,但 WordPress 认为我们正在使用版本 7.4?

使用自定义插件中的 WooCommerce 模板发送自定义邮箱

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

在WooCommerce单产品页面中显示特价产品的节省金额

WooCommerce – 在新订单邮箱通知中显示产品分类计数

如何设置Mailersend PHP API 模板变量?

每个发布请求 Laravel 9 的 CSRF 令牌不匹配

如何在 Laravel 9 中判断异常是否可报告?