简体   繁体   中英

php recursive include function in 2 file with different path

Hi all I've a problem with include function in php:

I have 4 file:

dir1/file1.php
dir4/dir2/file2.php
dir3/file3.php
dir3/file4.php

In file1.php I have:

include_once('../dir3/file3.php');

In file3.php I have:

required('../dir3/file4.php');

In file2.php I want write:

include_once('../../dir3/file3.php');

but the required function in file3 doesn't work because the path of file4 must be

../../dir3/file4.php 

and not

../dir3/file4.php

How can I fix it?

You can use only dot (.) before your filename which will find that file from root of dir..for eg ./dir3/file4.php but it increase the overhead..Another way is to use

$base = __DIR__ . '/../';

require_once $base.'_include/file1.php';

If you are calling file3 from file2 you will have to go back 2 directories. The best way is using the full path like :

home/mysite/public_html/dir3/file3.php

It maybe (is) troublesome but good uptill some level. Edit: DIR and rest is also handy, depending on your need

I FIX it with:

In file1.php I have:

$path = '..';    
include_once($path.'/dir3/file3.php');

In file3.php I have:

required($path.'/dir3/file4.php');

In file2.php I want write:

$path = '../..'
include_once($path.'/dir3/file3.php');

This work for me.

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