unable to include when file is in directory
Moderator: General Moderators
unable to include when file is in directory
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)
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)
Hey man,
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
to include functions.php from here you need to use:It does not work like this:
root/main/functions.php
root/testdir/test.php
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
I had already tried that with the "../" it didn't work, this is what I have in my apache
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
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>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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Your Apache setup should not affect the way your relative paths work.
... is the same as ...
Code: Select all
<?php
include('main/file.php');
?>Code: Select all
<?php
include('./main/file.php');
?>Have you tried ./../main/functions.php?tbbd wrote:I have tried many different ways:
main/functions.php
/main/functions.php
./main/functions.php
../main/functions.php
Just use the full path and be done with it.
Code: Select all
<?php
include('/home/username/httpddocs/includes/main.php');
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I would recommend defining constatns for key parts in your system.
For example in root you have config.php
then assuming each request loads config.php
in your test file you will have something like.
Then it does not metter where test.php is located. it will always find main.
For example in root you have config.php
Code: Select all
//in root/config.php file
define("ROOT_PATH",dirname(__FILE__));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");