![](/img/trans.png)
[英]Can I change current directory to parent directory using PHP __DIR__ constant?
[英]PHP get previous directory path of current directory using magic constants __DIR__
我的共享主机中有自定义 cron 任务,我必须使用 php 包含我的文件。 现在我的文件夹结构是示例:
domain.com
public_html
parser
regions
other.php
parser.php
我在other.php
文件中工作。 当我使用require
方法需要我的文件时:
require "../parser.php";
收到错误信息:
PHP 致命错误:require(): 需要打开失败 '../parser.php'
我如何使用 PHP 魔术常量__DIR__
获取当前目录的先前目录路径以包含我的文件parser.php
?
你可以简单地做:
require __DIR__ . '/../parser.php';
__DIR__
返回写入文件的绝对路径,不带尾部斜线。 所以在它之后,您可以简单地将相对路径附加到文件(带有前导斜杠)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.