Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

relative path - How to require PHP files relatively (at different directory levels)?

I have the following file structure:

rootDIR
    dir1
        subdir1
           file0.php
           file1.php
    dir2
       file2.php
       file3.php
       file4.php   

file1.php requires file3.php and file4.php from dir2 like this :

require('../../dir2/file3.php')

file2.php requires file1.php like this:

require('../dir1/subdir1/file1.php')

But then require in file1.php fails to open file3.php and file4.php ( maybe due to the path relativeness)

However, what is the reason and what can I do for file2.php so file1.php properly require file3.php and file4.php?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For relative paths you can use __DIR__ directly rather than dirname(__FILE__) (as long as you are using PHP 5.3.0 and above):

require(__DIR__.'/../../dir2/file3.php');

Remember to add the additional forward slash at the beginning of the path within quotes.

See:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...