简体   繁体   English

PHP 使用魔术常量 __DIR__ 获取当前目录的上一个目录路径

[英]PHP get previous directory path of current directory using magic constants __DIR__

I have custom cron task in my shared hosting and I must include my file using php.我的共享主机中有自定义 cron 任务,我必须使用 php 包含我的文件。 Now my folder structure is sample:现在我的文件夹结构是示例:

domain.com
  public_html
    parser
      regions
        other.php
      parser.php

I work inside other.php file.我在other.php文件中工作。 When I require my file using require method:当我使用require方法需要我的文件时:

require "../parser.php";

getting error message:收到错误信息:

PHP Fatal error: require(): Failed opening required '../parser.php' PHP 致命错误:require(): 需要打开失败 '../parser.php'

How I can use PHP magic constants __DIR__ to get previous directory path of current directory for include my file parser.php ?我如何使用 PHP 魔术常量__DIR__获取当前目录的先前目录路径以包含我的文件parser.php

You can simply do:你可以简单地做:

require __DIR__ . '/../parser.php';

__DIR__ returns the absolute path to the file it was written in, without a trailing slash. __DIR__返回写入文件的绝对路径,不带尾部斜线。 So after it, you can simply append the relative path to the file (with a leading slash).所以在它之后,您可以简单地将相对路径附加到文件(带有前导斜杠)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM