I want my .php file categorize inside the folder. I have this root directory that contains folder and php file as below .
core
initial.php // database connection
css
style.css
js
js.css
apply
index.php
apply.php
templates
head.php
footer.php
index.php
My root index.php, I just include the file as usual. Note* - head.php and footer.php contains HTML file.
<?php
require_once 'core/initial.php';
include 'templates/head.php';
echo '<a href='apply/index.php'>Apply</a>';
include 'templates/footer.php';
?>
But my problem is, my index.php file inside apply folder cannot call initial.php.
Warning: require_once(core/initial.php): failed to open stream: No such file or directory in C:\xampp\htdocs\movement\production\index.php on line 2
Fatal error: require_once(): Failed opening required 'core/initial.php' (include_path='\xampp\php\PEAR') in root\test\apply\index.php on line 2
My apply/index.php code
<?php
require_once '../core/initial.php';
include '../templates/overall/header.php';
// PHP code here
include '../templates/overall/footer.php';
The reason is I don't want my .php file all located in root directory. I want each php file except index.php in their own folder.
How can I do that ? Can someone help me on this problem ?
Thanks.
In
initial.php
or some other helper file, do:Now you only have to get a single include right; the file that holds the function. Always include it using a full path so it will work from anywhere:
You can go even further and make use of the
auto_prepend_file
directive which tells PHP to always execute that file before running the regular script. That's a great file to have the function above, and PHP will include it for you automatically.Anyway, once the file with
getPath()
is included, you never have to worry about using the right path again. Just make sure all include directories are listed in theINCLUDE_DIRS
constant and include/require your files like this:Note an important limitation of this approach is that if you have two files by the same name, it won't work because the first one found will be included. In that case, just be sure to always use the full path when including/requiring starting with
$_SERVER['DOCUMENT_ROOT']