views:

60

answers:

4

I realized I used a particular function over and over again in my PHP files, so I took the function out of all of them and put it in a seperate PHP file and included it where I need it.

Now when I make updates to the function, all the files that use it get updated. Great.

Now there is just one problem, the function itself uses relative URLs, and when you include the PHP the files are relative to the calling PHP, not the included's PHP directory. I know the quick and dirty answer is to use absolute links, but how can I get the included PHP to reference things to ITSELF and not to the PHP that called it.

Example:

index.php includes a file called create_table.php..... create_table.php echoes CSS and javascript to index.php. The CSS and javascript are in the same directory as create_table.php, but when using src="javascript.js" it wants to find the javascript.js where index.php is located, not where create_table.php is located.

Edit: how the structure works

/includes/create_table.php
/includes/style.css
/includes/javascript.js
/test/dir/1/2/3/index.php

So when I attempt to use src="javascript.js" instead of looking for /includes/javascript.js it tries to find /test/dir/1/2/3/javascript.js

+1  A: 

Use of absolute links is not "quick and dirty" but absolute necessary thing.
And your problem has very little in common with PHP. It is HTML problem. It is not PHP calling your CSS file, but browser, after reading the link from HTML code.

The only purpose of absolute path is to solve your problem - to find a particular file from any location.
Of course it should be only path, not URI. All HTML links can be (and should be) shortened to path parts.

instead of looking for /includes/javascript.js

Bingo!
Therefore, just tell it to look for this very address, an absolute one - /includes/javascript.js

Col. Shrapnel
+1 Yes, bingo! :) If anything, it's the other way round - relative URLs are "quick and dirty" IMO. Although, as mentioned in comments below, strictly an absolute URL contains the full domain. This is a root-relative URL.
w3d
A: 

I think you're going to have to ultimately use absolute paths. The question is how to get those absolute paths without hard coding it...

You can use the magic constant __FILE__ to get the absolute path of the current script (ie. create_table.php) and then do something like:

$filename = realpath(dirname(__FILE__).'/../../css/mystyles.css');

To get the absolute path of a file you know as being relative to the current script. (ie. up two directories and down into css/)

Or you could start the other way and use $_SERVER['DOCUMENT_ROOT']. Assuming you know all the paths relative to your webroot. This would be my preferred way.


EDIT: I think you can pretty much ignore the above! I was assuming (before your edit) that you were literally reading your CSS and JavaScript files server-side and adding the contents of to index.php - I guess not!

To re-answer... I would never use relative pathsURLs for client-side CSS and JavaScript because of exactly this problem. Some people always use absolute URLs, personally I prefer root-relative URLs. If you are generating this URL server-side, it doesn't much matter.

Root-relative URL: /includes/style.css

What do you perceive is wrong with this? I would have said this was preferred.

w3d
Are you sure this code would help? ;-) What's the use of this $filename variable?
Col. Shrapnel
Ah, the question has been modified since I first answered! "echoes CSS and javascript to index.php." - implied that the CSS and JavaScript were read and literally inserted into index.php! It looks like they are being linked to after all! ($filename is the absolute filename of the mystyles.css file for which you know the relative path to the current script.)
w3d
"Root-relative" path usually called "absolute". Do not mess *path* with *URL*.
Col. Shrapnel
Sorry yeah, I've corrected 'relative paths' to 'relative URLs' in the above. But "root-relative" and "absolute" URLs are _not_ the same. Absolute URL: `http://www.example.com/includes/style.css` whereas root-relative URL is: `/includes/style.css` (influenced by the `BASE` element for instance)
w3d
A: 

There is likely a better way to handle whatever you're trying to do rather than dynamically require files from within PHP functions.

At the very least, absolute paths is key, and generally absolute from root, like: require_once("/lib/fileOne.php");

not: require_once("lib/fileOne.php");

Ben Guthrie
This appears to be a client-side URL issue rather than a server-side path issue as it happens.
w3d
A: 

So you are saying there is a file that always get required. In this file, have a function that compares __FILE__ to SCRIPT_FILENAME. So, for example, you know includes/create_table.php's path to your root directory is ../ so it has a folder difference of 1, if the path was was includes/foo/create_table.php, the path to root would be ../../ so a difference of 2, etc...

$dif_to_root=1;

for
(
$i=0,$path='';
$i<($dif_to_root-(count(explode('/',__FILE__))-count(explode('/',getenv('SCRIPT_FILENAME')))));
$i++
)$path.='../';

Now just have in your code src="$path.includes/style.css"

fabjoa