In need of a fix (PHP)

Tindris

Seasoned Veteran
Joined
Mar 6, 2011
Messages
3,967
Reaction score
9
FP$
28,386
Basically I am working on a website that uses subfolders for example yourwebsite.com/foldername/index.php. This obviously causes issues when calling files with PHP so I am using
Code:
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
?>
to call the include files which works fine.

The problem I am having is when it comes to calling the JS, CSS and images. How would I go about using something similar so when I source and image or file it starts straight from the root directory? Grateful for any help with this!
 
if use ' /' infront when linking css or js files it would start from root . for example '/foldername/style.css' would just look for style.css from root .

If you want to the variable $root to be used in css and js files for eg :
Code:
background : url('<?php echo $root;?>/folder/sub-folder/image.jpg');
the you may rename try renaming style.css to style.php and setting correct headers in style.php file like :
Code:
 <?php header('Content-type: text/css; charset=UTF-8'); ?>
//give normal css below
 
(Without knowing full code)
As Amalfra said.
Put a / before all images.
Code:
background:url('/images/image.php');

Then when getting the style just add ../
Code:
<link rel="stylesheet" type="text/css" href="../style.css" />
(same for javascript 😛 )
For each folder you need to do just add another ../
like say the new file is: example.com/folder/anotherfolder.
Just add ../../style.css.
 
In this situation that doesn't work, it needs to be dynamic. I have a header.php file in the main directory. In that file it calls the CSS files. In a subfolder I have a file which includes the same header.php from the main directory. When calling this it doesn't call the CSS. I need a PHP query which always directs the browser to look for the CSS from the root wherever the calling file is located.
 
If your header is located in

Code:
yourwebsite/inc/header.php

And your css file is located in

Code:
yourwebsite/css/style.css

And the header gets included by a normal index file

Code:
yourwebsite/index.php

You will need to include your css like this:

Code:
<link rel="stylesheet" type="text/css" href="css/style.css" />
 
Back
Top Bottom