projectRoot

Hey

I am using this code at the top of my sites pages to sort out some file path issues:

	$projectRoot = dirname(__FILE__);
	require_once($projectRoot.'/includes/top.php');

However, when I get to linking to a stylesheet I write it like so:

<link rel="stylesheet" href="<?php echo $projectRoot.'/styles/styles.css'; ?>" type="text/css" />

and it REFUSES to use the stylesheet even though the path is CORRECT.

I have figured out that the only way it will work is if the WHOLE path uses FORWARD slashes rather than backslashes.

Is there anything I can do to solve this issue?

Hi Neil
Have you set a base href in the HTML?


<base href="http://localhost/mysite" />

To be honest, I have NEVER even heard of the <base> tag before?

What does it do?

The base href tag tells the browser that all relative links within the document start from that specified base location.

ok, As I develop on my local machine and then upload to my webhost. Would that make a difference?

and can I use this when including other files via php?

yes you would need to change the tag when you upload it. It should work with any relative path…

hmm,

If I didn;t use the <base> tag, and kept with using the $projectRoot variable I ahve been using up until now, is there something I could run to swpa the direction of the slashes so the path would be corrct for the stylesheet links?

It will never work. $projectRoot contains the system file path for example C:\User\some\path\here This will not work for a web site. You need to cut off the part the relates to the web root where. The web root would be IANA — Example domains the root is the last slash “/” so example.com/index.php would be at the web root.

Hmmm, So i’m basically back where I was the other day when I posted about this file path issue.

Maybe you guys in this thread can find me a decent solution.

Here’s the situation:

I have my webroot folder - “www”

Inside that, each folder is a single project or website.

Those project folders each contain for example:

includes
styles
scripts
index.php
about.php
contact.php
shop.php

The includes folder has both a top.php and a bottom.php file which are icnluded into each of the main site pages at the top and bottom respectively.

The styles.css and script.js pages are linked to from the top.php include and are stored in there respective folders

So basically, top.php includes the styles and the scripts and the site pages include top and bottom.

Now, that works fine for the site but then, I added a new folder in the project called “admin”.

This folder houses all the pages for the CMS for that particular site. in there I ahve the main CMS pages and on those I too want to include the sites top and bottom pages so the CMS keeps the same style as the main site. I can include the top and bottom pages but what fails is that the file path to the styles.css and script.js files is now incorrect as we are another folder into the folder hierarchy.

I basically want it set up so that it just works.

Any ideas?


$path  = preg_replace( '~[\\\\\\\\/]+~', '/', $_SERVER['DOCUMENT_ROOT'] . '/' ); # C:/Some/Directory/wwwroot/
$path2 = preg_replace( '~[\\\\\\\\/]+~', '/', __DIR__ . '/' );  # C:/Some/Directory/wwwroot/ProjectDir/
$path3 = str_replace( $path, '/', $path2 ); # /ProjectDir/

Quickly thrown together may need more work.
The preg_replace normalizes the slash and remove extra ones.

Thanks for that. Could you please just explain what that will do?

Updated my post some. Does the help clear it up?

So, If I prepended $path 3 to the path to my stylesheet in the top.php page, will it work from the main site pages and the pages within the admin folder?

Extra question in addition to the one in my previous post.

How do other people overcome this issue? Surely i’m not the only one to hit it?

Well I don’t have your setup so I cannot go and test for you.
Just remember it is for URLs not for file paths like PHP would use.

Right,

I edited one of my site pages to test it as follows:

<?php
	$path  = preg_replace( '~[\\\\\\\\/]+~', '/', $_SERVER['DOCUMENT_ROOT'] . '/' ); # C:/Some/Directory/wwwroot/
	$path2 = preg_replace( '~[\\\\\\\\/]+~', '/', __DIR__ . '/' );  # C:/Some/Directory/wwwroot/ProjectDir/
	$path3 = str_replace( $path, '/', $path2 ); # /ProjectDir/ 
	require_once($path3.'includes/top.php');
?>

It fails to locate top.php at all using that

That is because its not for PHP to use, it is for your links in HTML.
Use your current method of $projectRoot for PHP but use the method I provided for HTML links.

So, I should keep php using what I had before with $projectRoot and use this for the standard links to css and js?

Right,

I have written the following:

$wwwRoot = $_SERVER['DOCUMENT_ROOT'];
	$toCurrentFolder = dirname(__FILE__).'/';
	$folder = 'lymmog'; /*basename($toCurrentFolder);*/
	$fullPath = $wwwRoot.$folder.'/';

That works perfectly so I can include files like:

require_once($fullPath.'includes/top.php');

However when I try referencing my CSS files like:

<link rel="stylesheet" href="<?php echo $fullPath.'styles/styles.css'; ?>" type="text/css" />

It fails, no styles are being applied.

The file path in the source code is 100% correct:

<link rel="stylesheet" href="D:/Files/wamp/www/lymmog/styles/styles.css" type="text/css" />

If it works with referecning the PHP includes like that they why doesn;t it work for the stylesheet referencing?

And just to add, this is the outcome of those 4 new lines of code:

wwwRoot = D:/Files/wamp/www/
toCurrentFolder = D:\Files\wamp\www\lymmog/
folder = lymmog
fullPath = D:/Files/wamp/www/lymmog/

Neil

<?php

	$host = 'http://'.$_SERVER['HTTP_HOST'];

	$directory = dirname($_SERVER['SCRIPT_NAME']);

	echo $directory == '/' 
		? $host.'/' 
		: $host.$directory.'/';

?>

My guess based on your drive path it would be: http://localhost/lymmog/