How to use __dir__?

后端 未结 1 1287
-上瘾入骨i
-上瘾入骨i 2020-12-03 07:05

I want to use __dir__.

However, I can\'t find any good tutorial on how to set it up. I have my htdocs in Dropbox.

Does it work something like th

相关标签:
1条回答
  • 2020-12-03 07:20

    You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.

    Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.

    project
    ├── inc
    │   ├── file1.php
    │   └── file2.php
    └── index.php
    

    If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:

    <?php
    include __DIR__ . "/file2.php";
    

    To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:

    <?php
    include __DIR__ . "/../warlock.php";
    
    0 讨论(0)
提交回复
热议问题