bikey77
bikey77

Reputation: 6672

PHP - make paths relative when including file

In a site root I have the following folders and files:

enter image description here

File includes/scripts.php includes links to all css/js files needed for the rest of the pages to function. When including scripts.php from any file in the site root (ex cart.php) all paths work fine. But when including from within a folder (ex. /admin) the paths are not correct, obviously because there isn't a /js or /css dir inside /admin.

I tried several things like changing the paths inside scripts.php from '../css/styles.css' to ./css/styles.css or just /css/styles.css but that didnt work. Also tried include($_SERVER["DOCUMENT_ROOT"] . "includes/scripts.php"); but also without success.

How do I make paths relative inside scripts.php so that no matter where it is included from, all css/js paths are correct and relevant to the site root?

This is an example of some linked files in scripts.php:

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.2.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.alerts.js"></script> 
<script type="text/javascript" src="js/cookie.js"></script> 
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" type="text/css" href="css/layout.css">

etc...

EDIT: I see that most of the answers have taken for granted that the files are hosted in a UNIX environment which is not true, it's being developed on a Windows/Apache setup and will be hosted on a UNIX server when finished. So I need a solution that works on both systems so I don't need to change the code whenever I'm editing/uploading the files to the live server.

Upvotes: 0

Views: 5086

Answers (4)

Ochui Princewill
Ochui Princewill

Reputation: 157

try using these function but you need to place it at the root folder on call it were you need it

function($_file){
php_uname();
$server_info = PHP_OS; // get host os info
$os = strtoupper(substr(PHP_OS, 0, 3));
if($os === 'WIN'){
    $path = dirname( $file );
    $p = explode(':',$path);
    return str_replace(array('\\','\\'), array('/','/'), $p[1]); //return the absolute path
}else{
    return dirname( $file );
}
}

example:

include get_path(__DIR__).path/to/asset/file.js

Upvotes: 0

h2ooooooo
h2ooooooo

Reputation: 39550

Includes in PHP

Method 1 - using a constant

Create a constant (eg. ROOTDIR) in one of your root files (eg. index.php, or create a file that you always include relatively before any other include) before you include any other files:

//Place this in a file you always include relatively
define("ROOTDIR", dirname(__FILE__) . "/");

and wherever you include stuff:

//Place this in whatever file
require_once(ROOTDIR . "admin/panels/users.php");

Method 2 - using set_include_path

Alternatively, in the same file, instead of the constants, you could use set_include_path as such:

//Place this in a file you always include relatively
set_include_path(dirname(__FILE__) . "/");

and when you include stuff:

//Place this in whatever file
require_once("admin/panels/users.php");

Method 3 - through the PHP config

If you'd rather be without constants and set_include_path and have access to the PHP config file, you could also change include_path in the config to eg. /var/www/, and then simply use above require_once when including files. More info about that here.


But what about inclusion of scripts and stylesheets, h2ooooooo?

Well, I knew you'd get into that. What I usually tend to do is either make the path relative to root by starting it with an /:

<script type="text/javascript" src="/js/jquery-1.8.0.min.js"></script>
<!--                                ^                     -->

or you can alternatively set a <base> path in HTML:

<head>
    <base href="http://www.mydomain.com/" />
    <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
</head>

Note: When setting a <base> path, it also specified that all links will be relative to the base path as well so if you have a link in the admin folder pointing towards subdir/ it will link to http://www.mydomain.com/subdir/ and not http://www.mydomain.com/admin/subdir/.

Upvotes: 6

Damien Legros
Damien Legros

Reputation: 529

Solution 1: Use the base tag in your html <head>, note: that will affect ALL the relative links

<base href="http://yoursite.com/" />

Solution 2: Define an environment variable in your htaccess file

SetEnv MYSITE_URL http://yoursite.com/

and then in your html echo that environment variable

<script type="text/javascript" src="<?php echo getenv('MYSITE_URL'); ?>js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="<?php echo getenv('MYSITE_URL'); ?>js/jquery-ui-1.9.2.custom.min.js"></script>
...

Solution 3: Similar to solution 2 but instead create a php constant with your site url and echo it in your html

Solution 4: Start your asset path with a /

Upvotes: 0

kapil
kapil

Reputation: 162

Please do the following changes instead of include($_SERVER["DOCUMENT_ROOT"] . "includes/scripts.php");

<script type="text/javascript" src="<?php echo $_SERVER["DOCUMENT_ROOT"];?><js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="<?php echo $_SERVER["DOCUMENT_ROOT"];?>js/jquery-ui-1.9.2.custom.min.js"></script>
<script type="text/javascript" src="<?php echo $_SERVER["DOCUMENT_ROOT"];?>js/jquery.alerts.js"></script> 
<script type="text/javascript" src="<?php echo $_SERVER["DOCUMENT_ROOT"];?>js/cookie.js"></script> 
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER["DOCUMENT_ROOT"];?>css/styles.css">
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER["DOCUMENT_ROOT"];?>css/layout.css">

Upvotes: 1

Related Questions