Page 1 of 1

unable to include when file is in directory

Posted: Mon May 15, 2006 4:47 am
by tbbd
I have a file, called "test.php", it is located in the root folder, there is a directory called main, inside that directory is a file called "functions.php"
so, in test.php, I have this line include_once("main/functions.php"); it works great.
If I put the test.php file inside a directory and try to do the include, it says the filecan't be found.

so it works like this:
root/main/functions.php
root/test.php
include the "functions.php" from test

It does not work like this:
root/main/functions.php
root/testdir/test.php
include the "functions.php" from test


I have tried many different ways:

main/functions.php
/main/functions.php
./main/functions.php
../main/functions.php

and if I try the full url, http://localhost/main/functions.php, apache crashes
(even when I do this when the test file is in the root folder)

Posted: Mon May 15, 2006 7:54 am
by BadgerC82
Hey man,
It does not work like this:

root/main/functions.php
root/testdir/test.php
to include functions.php from here you need to use:

include_once("../main/functions.php");

That should definitly work... If not you may need to double check your directory structure and possibly even ur apache / php settings.

do you have:

DocumentRoot c:/VirtualHosts/DOCS/testdir/? in your apache for example?

Hope thats of some help

Posted: Tue May 16, 2006 1:46 am
by tbbd
I had already tried that with the "../" it didn't work, this is what I have in my apache

Code: Select all

<VirtualHost 192.168.1.100>
ServerAdmin myemail@mydomain.com
DocumentRoot "c:/websites/mt"
ServerName mt
ServerAlias *.mt
ErrorLog logs/mt_error.log
ScriptAlias /cgi-bin/ "c:/websites/mt/cgi-bin/"
<Directory "c:/websites/mt">
Options All Includes Indexes
</Directory>
</VirtualHost>
so the full path to the file I want to include: http://mt/main/functions.php
full path of the file I have the include statement in: http://mt/testdir/test.php

The reason I wanted to place the file in a folder is I have about 40 or so, all in the root folder (because it works from there), and I want to organize them

I know it is a setting on my end, when I upload the files to a webhost, it works there

Posted: Thu May 18, 2006 12:14 am
by tbbd
nobody has any idea ha?
I looked thru all the apache docs for virtualhost, thinking it may be a setting there, but all I found was instructions on how to set one up and the different kinds, no listing of the different <> you can add in each and what they do.

Posted: Thu May 18, 2006 12:50 am
by RobertGonzalez
Your Apache setup should not affect the way your relative paths work.

Code: Select all

<?php
include('main/file.php');
?>
... is the same as ...

Code: Select all

<?php
include('./main/file.php');
?>
tbbd wrote:I have tried many different ways:

main/functions.php
/main/functions.php
./main/functions.php
../main/functions.php
Have you tried ./../main/functions.php?

Posted: Thu May 18, 2006 5:32 am
by Benjamin
Just use the full path and be done with it.

Code: Select all

<?php
include('/home/username/httpddocs/includes/main.php');
?>

Posted: Thu May 18, 2006 10:03 am
by RobertGonzalez
I like agtlewis' answer better than mine. You should use the full path. That way you know you are always pulling from the correct location.

Posted: Fri May 19, 2006 2:00 am
by jmut
I would recommend defining constatns for key parts in your system.

For example in root you have config.php

Code: Select all

//in root/config.php file

define("ROOT_PATH",dirname(__FILE__));
then assuming each request loads config.php
in your test file you will have something like.

Code: Select all

//root/testdir/test.php

require_once("config.php");
require_once(ROOT_PATH."/main/somefile.php");
Then it does not metter where test.php is located. it will always find main.