So I I've been creating this HEADER
and today I included it in a file which wasn't in the same folder as the other .php
files. This resulted in the path from the includes inside the HEADER
wasn't the right once.
I was wondering if there was a dynamic pathfinder way to solve this?
In the HEADER
I have this
<script type="text/javascript" src="./js/togglemenu.js" defer></script>
and was wondering if it would be possible to create a function which includes the path instead like
<script type="text/javascript" src="dynamicInclude(js/togglemenu.js)" defer></script>
I've tried different things, but can't seem to understand how or even if this is possible.
$current_file_path = dirname(__DIR__);
//Writes include(C:\xampp\htdocs\metrics\website\..\togglemenu.js)
//Want to find 'togglemenu.js' no matter which file this line is included,
//so I don't have to ../../ my way through every file to find the correct path each time.
include(dirname(__DIR__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'togglemenu.js');
EDIT
Small edit with more information:
These is my files
In my website\index.php I have
<?php
declare(strict_types=1);
include("./includes/header.php");
?>
<h2>Home site</h2>
This works great.
BUT if I write the same include in my website\404\index.php I get error, because the include actually has to be ../includes/header.php
. This is where I was wondering if I could make a function like dynamicPageLoad(header.php) which would return the right file path no matter which of these two files this function was used.
I did try out the comment with src="/js/togglemenu.js"
, but didn't work for me.
Update
Might have found a way to do this? Feel free to change the code if there is a better way to do this
function get_file_dir($path) {
global $argv;
$dir = dirname(getcwd() . '/' . $argv[0]);
$curDir = getcwd();
chdir($dir);
$dir = getcwd();
chdir($curDir);
return $dir.$path;
}
include(get_file_dir('\includes\header.php'));