Skip to content
Advertisement

Dynamic pathfinder for includes

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:xampphtdocsmetricswebsite..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

files

In my websiteindex.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 website404index.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.

enter image description here

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('includesheader.php'));

Advertisement

Answer

One possible solution is to use a base tag. This sets the base URL for every href in the page:

<base href="https://www.example.com">

Change it to your base URL. Now you only have to set it once in the page – either set it statically or dynamically. All other relative links (hrefs) are then relative to that base URL.

In a PHP application you would typically create separate configurations for your different environments like live and development. Good thing is you only have to print it once in the page and all relative hrefs will work without a problem.

You should also know to avoid a common mistake: With a base href of https://www.example.com like above all hrefs must have / at the start. If you include the / in the base href you must not have a beginning / in all hrefs.
You can check the network tab in the browser’s development tools to see if the URLs work correctly.

If you do not have such a setting, check this answer on how to print the base URL dynamically:
How do I get the base URL with PHP?

Documentation: HTML element base

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement