How to require PHP files relatively (at different directory levels)?
Asked Answered
J

5

60

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?

Joline answered 18/10, 2012 at 12:24 Comment(0)
L
79

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:

Luciennelucier answered 11/8, 2013 at 13:19 Comment(0)
A
56

Try adding dirname(__FILE__) before the path, like:

require(dirname(__FILE__).'/../../dir2/file3.php');

It should include the file starting from the root directory

Arapaima answered 18/10, 2012 at 12:38 Comment(3)
If you're on 5.3 or higher you would better use DIR, the same as dirname(FILE) but saving you the 'costly' dirname() call.Votyak
thanks..can you tell me the difference between '/../dir' and '../dir'Joline
in this case you use /../ because dirname(FILE) return a string like: '/root/dir1/dir2/dir3' , missing the last '/', so without it merging the 2 paths it would become '/root/dir1/dir2/dir3../' .Arapaima
N
8

A viable recommendation is to avoid "../../" relative paths in your php web apps. It's hard to read and terrible for maintenance.

  1. As you can attest. It makes it extremely difficult to know what you are pointing to.

  2. If you need to change the folder level of your application, or parts of it. It's completely prone to errors, and will likely break something that is horrible to debug.

Instead, a preferred pattern would be to define a few constants in your bootstrap file for your main path(s) and then use:

require(MY_DIR.'/dir1/dir2/file3.php');

Moving your app from there, is as easy as replacing your MY_DIR constants in one single file.

If you must keep it relative. At a minimum, construct an absolute path based on a relative path reference, like the accepted response suggest. However also strongly keep in mind the importance of naming your ../ anonymous intermediate paths, as a means to remove confusion or ambiguity.

Nicholnichola answered 3/3, 2015 at 2:52 Comment(2)
Irrelevant to the questionForbis
@Gershy It's a relevant answer in my book, giving "a viable alternative", with a "try this instead" recommendation. Not every answer is a direct one. I improved my answer however.Nicholnichola
V
1

You can always use the $_SERVER['DOCUMENT_ROOT'] as a valid starting point, as well, rather than resorting to a relative path. Just another option.

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
Vshaped answered 7/5, 2019 at 14:14 Comment(1)
Not available in CLI or Cron.Yarn
B
-4

I think your cwd is dir2. Try :

require("file3.php");
require("file4.php");
Blackness answered 18/10, 2012 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.