0

My PHP files within folders are not rendering my default CSS files.

I have a "document-head.php" file that contains:

<link type="text/css" rel="stylesheet" href="assets/css/main.css"/>

Within each file I include this 'document-head.php';

For example, the directory I have is:

index.php
|__ assets
    |__ css
        |__ main.css
|__ includes

    |__ document-head.php

|__ components
    |__ secondary.php

|__ partials

Within "components/secondary.php" contains

 <?php include('../_includes/document_head.php'); ?>

And within "index.php" contains:

<?php include('_includes/document_head.php'); ?>

How do I make sure that files within folders that include this "document_head.php" always render CSS/JS files?

Edit: The reason I can't use absolute URL's is that one of the developers configured a script that allows me to upload my application to an FTP where I can name a new folder. For example, if all my files are in "secret-app", in the terminal I can publish my app to an FTP with the new folder "test-app" and it will create "mydomain.com/test-app/secret-app". (It's a ghetto system, but it's useful for my particular needs.)

5
  • Is it a problem to set the domainname in the href? Commented Dec 17, 2013 at 2:42
  • Use a setting / config file to set the absolute path prefix, and load the config file in every php ( normally I call the file require_once.php ) Commented Dec 17, 2013 at 2:43
  • Why can you not use absolute URLs? /assets/css/main.css? Commented Dec 17, 2013 at 2:43
  • The reason I can't use absolute URL's is that one of the developers configured a script that allows me to upload my application to an FTP where I can name the new folder. For example, if all my files are in "secret-app", in the terminal I can publish my app with the new folder "test-app" and it will load "mydomain.com/test-app/secret-app". (It's a ghetto system, but it's useful for my particular needs. Commented Dec 17, 2013 at 2:45
  • You need to figure out the base url then, this is possible via PHP ($_SERVER variables). In the HTML you could use absolute url's, making use of the base tag in the head of your document (containing the base url to your app, mydomain.com/test-app/secret-app/) - see w3schools.com/tags/tag_base.asp Commented Dec 17, 2013 at 2:57

2 Answers 2

1

Try change to an absolute href url like:

<link type="text/css" rel="stylesheet" href="/yourApp/assets/css/main.css"/>

If you're using a framework, you will have several ways to get your base_url.

Sign up to request clarification or add additional context in comments.

4 Comments

Edited for why I can't use absolute paths.
any way you can get the base_url using $_SERVER['SERVER_NAME']
When I test locally, it is "localhost" but when I upload to my server it is "mydomain"
If you put only / you don't need to specify your local or remote domain, only you need to have the same path in both servers to works.
0

if this stylesheet is within the document_head.php

<link type="text/css" rel="stylesheet" href="assets/css/main.css"/>

then try this :

<link type="text/css" rel="stylesheet" href="../assets/css/main.css"/>

sorry for my grammar :D

Comments

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.