Jump to content

[SOLVED] about including files


vividona

Recommended Posts

Hi friends,

 

I don't know how to explain this.

 

I am confusing when regarding including files.

 

suppose I have these stuff in my script:

folder1/test1.php

 

folder2/test2.php

 

index.php

 

if I need to include test1.php file to be inside test2.php file, I use this function

require"../folder1/test1.php"; // (correct right???).

 

if I need to call test2.php to be available in index.php, I use this

 

require("folder2/test2.php"); // (correct right???).

 

but the problem it will refuse the first path which we include first.

require"../folder1/test1.php";

 

bcs "../" means from folder to folder

 

how can I make all paths readable???

Link to comment
Share on other sites

let's pretend that you have this folder structure

applications/index.php
test/config.php

 

to include the config file you would do something like this:

include_once('../test/config.php');
where ../ = one folder bellow

 

more informations about include_once require_once include and require you can find on http://www.php.net/manual

here it so for the include_once http://www.php.net/include_once

 

if you have two files in folder let's say:

applications/test.php
applications/index.php

you will do like this 

include_once('index.php'); in test.php file

 

Link to comment
Share on other sites

i'm a relative noob to php, but i've had zero issues with includes/requires when the paths are relative from the app's document root. i figure if it's going to search for includes from one or more directories, the doc root will be one of them. so far so good. try making your paths relative to the doc root and see what happens.

 

jason

Link to comment
Share on other sites

Include locations are relative to the script being executed, not the one being included.

 

Which is why using absolute path's (they can be dynamic using $_SERVER['DOCUMENT_ROOT'] are often the better way to go when including a file. Your issue here is a prime example of why you would want to use absolute paths instead of relative.

 

Meaning that if you are including test2.php inside of index.php any includes in test2.php would have to act like they are coming from the folder that houses index.php if that makes sense.

Link to comment
Share on other sites

Hi friends,

 

I don't know how to explain this.

 

I am confusing when regarding including files.

 

suppose I have these stuff in my script:

folder1/test1.php

 

folder2/test2.php

 

index.php

 

if I need to include test1.php file to be inside test2.php file, I use this function

require"../folder1/test1.php"; // (correct right???).

 

if I need to call test2.php to be available in index.php, I use this

 

require("folder2/test2.php"); // (correct right???).

 

but the problem it will refuse the first path which we include first.

require"../folder1/test1.php";

 

bcs "../" means from folder to folder

 

how can I make all paths readable???

First of all, why your require syntax is different in those 2 scripts (you use only "" and then (""))?

Just stick to one syntax, it's just better for reading the code.

 

When you are using "../" it goes outside the folder you are right now, so ex. you have "server/public/folder1" and your script is in "server/public/index.php", the command "../" says go to "server/" folder.

 

Just corrent first one without "../" so:

 

require"folder1/test1.php"; //

Link to comment
Share on other sites

Include locations are relative to the script being executed, not the one being included.

 

Which is why using absolute path's (they can be dynamic using $_SERVER['DOCUMENT_ROOT'] are often the better way to go when including a file. Your issue here is a prime example of why you would want to use absolute paths instead of relative.

 

Meaning that if you are including test2.php inside of index.php any includes in test2.php would have to act like they are coming from the folder that houses index.php if that makes sense.

 

 

hiiiii

 

at last I found someone understand me

 

plz explain more

Link to comment
Share on other sites

yes fantomel

 

you are right

 

applications/index.php

test/config.php

main.php

 

will be like this

 

include_once('../test/config.php');

 

but what if I need to call this again to the main.php???

it will fail right???

 

i'm not sure where you want to point but we use include_once to include a file only once so yes if you include it again you will get an error :-?

Link to comment
Share on other sites

hiiiii

 

at last I found someone understand me

 

please explain more

 

In general I have a config.php file that houses all pertinent path information for my script by defining a constant.

 

(This would be in the config.php)

define("DIR_MAIN", $_SERVER['DOCUMENT_ROOT']);

 

So in general I always include this "config.php".

 

example of index.php

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/config.php");
include(DIR_MAIN . "/folder2/test2.php");
?>

 

Example of folder2/test2.php

<?php
include(DIR_MAIN . "/folder1/test1.php");
?>

 

Example of folder1/test1.php

<?php
echo "You have reached test1 from test2.php inside of an index file!";
?>

 

Folder structure:

index.php
includes\config.php
folder1\test1.php
folder2\test2.php

 

Should be what you are looking for.

Link to comment
Share on other sites

hiiiii

 

at last I found someone understand me

 

please explain more

 

In general I have a config.php file that houses all pertinent path information for my script by defining a constant.

 

(This would be in the config.php)

define("DIR_MAIN", $_SERVER['DOCUMENT_ROOT']);

 

So in general I always include this "config.php".

 

example of index.php

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/config.php");
include(DIR_MAIN . "/folder2/test2.php");
?>

 

Example of folder2/test2.php

<?php
include(DIR_MAIN . "/folder1/test1.php");
?>

 

Example of folder1/test1.php

<?php
echo "You have reached test1 from test2.php inside of an index file!";
?>

 

Folder structure:

index.php
includes\config.php
folder1\test1.php
folder2\test2.php

 

Should be what you are looking for.

 

ya I got the idea now

the problem is solved

thank you again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.