0

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?

1
  • If you figured it out yourself, you can post a answer if you want, don't write it in the question. (Added a answer, maybe it solves your problem too) Commented Feb 12, 2015 at 13:50

3 Answers 3

1

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';
Sign up to request clarification or add additional context in comments.

1 Comment

with include_once('./dir3/file3.php'); in file1.php not work. And I want use the php application in window e linux system.
0

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

1 Comment

sorry but the path can change with different OS. I want a portable php application.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.