如何相对要求PHP文件(在不同目录级别)? [英] How to require PHP files relatively (at different directory levels)?

查看:106
本文介绍了如何相对要求PHP文件(在不同目录级别)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下文件结构:


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

file1.php从dir2需要file3.phpfile4.php像这样:

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

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

file2.php需要这样的file1.php:

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

但是随后file1.php中的require无法打开file3.phpfile4.php(可能由于路径相对性)

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

但是,file2.php的原因是什么,我该怎么办,所以file1.php正确地需要file3.phpfile4.php?

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

推荐答案

对于相对路径,您可以直接使用__DIR__而不是dirname(__FILE__)(只要您是

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.

请参阅:

  • PHP - with require_once/include/require, the path is relative to what?
  • PHP - Relative paths "require"

这篇关于如何相对要求PHP文件(在不同目录级别)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆