0

I have a toolbarMenu.php that uses css in a css directory ("css") from the root folder.

All of my other PHP forms are in a root folder, and planned to re-organize all my PHP form to be move to a new folder called "forms" exluding toolbarMenu.php

After I move my PHP form to sub-directory, I can call this toolbarMenu.php by using include ex:

<?php include("../toolbar-top.php"); ?>

However, the CSS path broke because it's trying to look for the css folder inside the forms folder. I don't want to change the css path from my toolbarMenu.php to "../css/file.css" because I'm also using this php page with the website's root pages.

Is there a way to preserve the toolbarMenu.php's css path when it's being called from a PHP form from another sub-directory?

From Root folder:

Toolbar Menu path: "toolbarMenu.php"

CSS path: "css/cssFile.css"

PHP form calling the ToolbarMenu: "forms/form.php"

I hope I made my question clear :)

Thanks!

2
  • so the references to the css in toolbarMenu.php are broken because he is trying to access them from folder <forms>? Commented Aug 1, 2011 at 6:23
  • yes.. from my toolbarMenu.php.. the css path is "css/cssFile.css" since toolbarMenu.php is being called from another PHP form from a subdirectory "forms", that form is looking for "css/cssFile.css" but if I change it to "../css/cssFile.css" it works.. but I don't want to change it natively, because other page from the root directory uses it the toolbarMenu.php as well Commented Aug 1, 2011 at 6:27

3 Answers 3

4

You can either use an absolute path, eg

<link rel="stylesheet" type="text/css" href="/mySite/css/file.css">

or, if you want to keep it portable, set a dynamic <base> for relative paths. This way you can move your entire app to a sub-directory without breaking reference links, eg

<!-- in HEAD -->
<base href="http://localhost/mySite/">
<link rel="stylesheet" type="text/css" href="css/file.css">
Sign up to request clarification or add additional context in comments.

3 Comments

i'm currently using localhost. how would I use localhost as my base? I tried <base href="localhost/mySite"> but it didn't work
@Arnold Don't forget the trailing slash. Try <base href="http://localhost/mySite/">. I've updated my answer to match your environment better
this one works, adding the extra "/" at the end was the issue. thank so you much!
1

You have multiple options to get the desired result:

  1. You can define a prefix variable/constant at the top of every page which you then use in all your includes/urls/etc.
  2. You can also use the <base> tag in your header pointing to the absolute URL to ../ so something like http://ys.cc/proj/ if your forms directory is http://ys.cc/proj/forms

Comments

0

You can write your css file selector from:

css/file.css

to

/css/file.css

This will work in all folders.

2 Comments

@arnold-porche-villaluz The difference between those two is guaranteed behavior. If that didn't work, there's something wrong.
@Christian The OP confused us by saying the css directory was in the "root" directory. Looks like it's actually in a mySite sub-directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.