开发者

PHP Include calling other files

开发者 https://www.devze.com 2023-01-09 07:04 出处:网络
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.开发者_高级运维

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


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


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");


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.


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号