简体   繁体   中英

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. Now my folder structure is sample:

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

I work inside other.php file. When I require my file using require method:

require "../parser.php";

getting error message:

PHP Fatal error: require(): Failed opening required '../parser.php'

How I can use PHP magic constants __DIR__ to get previous directory path of current directory for include my file 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. So after it, you can simply append the relative path to the file (with a leading slash).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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