Reputation: 21371
Consider the following directory structure:
How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path.
<img src="<?php echo IMAGE_PATH; ?>">
which should be parsed as
<img src="images/logo.png"> <!-- if used in index.php -->
and
<img src="../images/logo.png"> <!-- if used in content.php -->
New Question (EDIT): Does root-relative path work when including php files using include / require methods?
Upvotes: 4
Views: 5935
Reputation: 655209
I would use something like an application base URL:
define('APP_URL', 'http://example.com/path/to/app');
echo '<img src="'.APP_URL.IMAGE_PATH.'">';
Or to have it more convenient, write a function that resolves your relative URL to an absolute URL.
Upvotes: 0
Reputation: 72510
You can use "root-relative" paths. Simply link to everything with a forward slash at the beginning, i.e.
<img src="/images/logo.png">
This will resolve to http://yoursite.com/images/logo.png
from every page on yoursite.com.
Upvotes: 3
Reputation: 69981
Absolute url or root paths will give you the least amount of headaces. Trust me, when the system grows you'll regret that setup.
It is a perfectly legal way to reference things. (as you ask in the comments)
If you're worried about setups between domains, just create a config variable with the absolute path to the domain / directory / etc
Upvotes: 3
Reputation: 11186
Try setting the <base>
tag in the <head>
section of your code.
All your images, css, and js files will use this instead of the url in the address bar.
Upvotes: 6
Reputation: 921
simply specify all paths as relative to the root
<img src="/images/logo.png"> <!-- will work anywhere -->
Upvotes: 1
Reputation: 253307
I'd suggest, primarily, that you use root-relative paths. This is only to reduce the complications of moving your site to another host, and also it allows for consistent paths (rather than using an if()
condition to test from where the script's being run).
But otherwise, your suggestion would be fine.
Upvotes: 0