Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Defining relative paths compatible with HTML Tags

Consider the following directory structure:

  • ROOT
  • ------ images
  • ............... logo.png
  • ------ includes
  • ............... vars.php
  • ------ layout
  • ............... content.php
  • ------ index.php

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?

like image 480
HyderA Avatar asked Dec 17 '22 06:12

HyderA


2 Answers

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.

Info on base

like image 69
Scott Avatar answered Dec 28 '22 06:12

Scott


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

like image 20
Ólafur Waage Avatar answered Dec 28 '22 08:12

Ólafur Waage