Help with relative paths

I am trying to re-organize all the web development apps I have built for myself (just simple ones like task timer, to do list, etc). The folder structure I would like to use is:
css (style.css, etc)
images
includes (header.php, footer.php, db_connect.php)
js

index.php for home page

Then a folder for each app:
timer (index.php, …)
todo (index.php, …)
etc.

My problem is that I want not only my home page, but also the index.php files in each of the app folders to be able to access the files in the includes folder. But my header.php file calls the style.css file from a different level, depending on whether the webpage is the home page, or one of the apps.

I use XAMPP, so all this is in a folder ‘business’ in my ‘htdocs’ folder. Is there some way I can make the path go back to the ‘business’ folder no matter how deep in the structure I am calling a file from, and then to the file I am wanting to call?

Yes. My preference is to set up virtual hosts on your local machine to match the remote setup, so that you don’t have to change your paths before uploading. I do this on Mac, but the process seems to be similar on PC. Check this out:

http://sawmac.com/xampp/virtualhosts/

Maybe I wasn’t very clear. This is going to stay on my local server, because it is just a set of tools that I use for business practice. I won’t be having a remote setup. I just don’t want to have to have each individual folder contain duplicates of the includes, images and css folders. But once I include header.php it will be included in different levels, so I run into problems with it finding the stylesheet.

What I said above still applies, but still, you could also avoid that and just work out what the actual system path to that folder. I don’t use Windows much, so this is just a guess at the path: DocumentRoot "C:/xampp/htdocs/business/"

I would expect something like this would work from any page in the site:

<?php include $_SERVER["DOCUMENT_ROOT"] . "C:/xampp/htdocs/business/"; ?>

Would that be a more acceptable way than something like this?

        if ($page_name == 'Home') {
            echo "<link rel=\"stylesheet\" href=\"css/style.css\" media=\"all\">";
        } else {        
            echo "<link rel=\"stylesheet\" href=\"../css/style.css\" media=\"all\">";
        }

You’d need a lot of different elses, so that seems inefficient to me. I prefer to establish the absolute path to a resource and always use that. It’s much easier.

1 Like

Okay. That makes a lot of sense. I will try it after I finish my work today. Thank you.

I tried it out and it turns out that $_SERVER["DOCUMENT_ROOT"] is the same as C:/xampp/htdocs , but I need to access “http://localhost/business/css/style.css” from both the top level of the site, as well as from one level down in the application folders.

what I would do is create an empty file in root called root.php

then do

if (file exists('./root.php')){
$dot = '.';
}
elseif (file exists('../root.php')){
$dot = '..';
}

echo '<link rel="stylesheet" href="'.$dot.'/layout6.css" type="text/css" media="screen">';

Add as many elseif’s required

I had to use [bracket] instead of a less than sign

You mean you forgot to enclose the < in backticks.

Or just created a whole code block. The easy way is to highlight the code and click the button in the post editor.

I’ve edited the post to that end.