1

I am currently working on a admin backend and now want to move some pages into different folders (so I do not have all scripts in one folder). Structure looks like this:

/
/theme/assets/...
/templates/head.php
/templates/foot.php
/top/index.php
/index.php

In head.php there are some stylesheets added like this

<link href="theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

This works fine for files in the root folder (e.g. index.php), but when I use the same head.php in /top/index.php it does not work because the path is wrong. Is it somehow possible to properly include the stylesheets etc. while having the scripts in different folders?


Greetz,

3 Answers 3

2

Just specify the root identifier (/) at the beginning of the href attribute:

<link href="/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
Sign up to request clarification or add additional context in comments.

5 Comments

First method does not work. Can I specify the "DOCUMENT_ROOT"? Because my interface is in a sub-folder.
You did not mention having a sub-folder in the question. You indicated everything was under /. Just add the sub-folder after the leading / in the first example, or the second method using: <link href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/SUB_FOLDER/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
You're using DOCUMENT_ROOT to construct an absolute path to the CSS file on the server, and that path is only meaningful on the server. Since <link> is a client-side link, your PHP code will generate something like this: <link href="/var/www/theme/.../font-awesome.min.css" />
The browser will try to access the CSS file at http://some-site.com/var/www/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css.
Ah, OK. I see what you mean.
0

Try this,

<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

In /top/index.php

OR

<link href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

This code can be used in any page. The $_SERVER['DOCUMENT_ROOT'] points to the root.

1 Comment

It's worth noting that'll only work to one level up. And also, index.php will not work with this method.
0

Use '../' to reference the folder above and work your way back. e.g

<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

1 Comment

It's worth noting that'll only work to one level up, and will break the index.php.

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.