Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

including files in php

Tags:

include

path

php

HI,
I try to run a file thru terminal but am getting the error like "include path is not correct"
for example, i have a "test.php" in following folder

/home/sekar/test/file.php  

in file php i've included a file "head.php" which is in ,

/home/sekar/test/includes/head.php

Thes head.php includes a class file called cls.php which is in class folder,

/home/sekar/test/classes/cls.php

i tried like this in terminal,

php /home/sekar/test/file.php

for a clear a view just have a look @ contents of the those three files,

file.php

<?php 
include_once "./test/includes/head.php";
?>

head.php

<?php 
include_once "./test/classes/cls.php";
?>

cls.php

<?php 
echo "this is from cls file";
?>

Can anyone help me to get around this issue? Thanks!

like image 748
vkGunasekaran Avatar asked Apr 21 '11 05:04

vkGunasekaran


1 Answers

I think that include_once() basically inserts code into your file without evaluating it, so the path is relative to that of the including file (file.php, not head.php).

Also, I'd do a bit of research on relative paths, as you're referencing from the directory /home/sekar/test/, not the file's path.

This might work:

file.php

<?php 
include_once "./includes/head.php";
?>

head.php

<?php 
include_once "../classes/cls.php";
?>

cls.php

<?php 
echo "this is from cls file";
?>
like image 61
Blender Avatar answered Oct 26 '22 15:10

Blender