mk_89
mk_89

Reputation: 2742

setting the absolute path for pages

I have been working on my site structure trying to make it less cluttered but I am running into issues with file paths.

site structure before

[folder] :   css
[folder] :   js
[folder] :   images
[folder] :   include
   |---> :   header.php
[file]   :   index.php
[file]   :   view_inventory.php
....

site structure after

[folder] :   css
[folder] :   js
[folder] :   images
[folder] :   include
   |---> :   header.php
[folder] :   view
   |---> :   inventory.php
[file]   :   index.php
....

As you can see I have decided to group view files placing them into their own folder

The issue with this is that both index.php and view_inventory.php use the include/header.php file

The include/header.php file has a header logo located at images/logo.php, the path of the images would not be problem if both index.php and view_inventory.php were in the same folder, but since they aren't the file in view/inventory.php will have the incorrect image path.

I have thought about defining full paths for files such as images (e.g. localhost/sites/bk/images/logo.png) but it would be a bad idea because I may decide to upload the files onto a server and the server address can constantly change..etc

What is the best way to tackle this problem?

Upvotes: 0

Views: 248

Answers (1)

JvdBerg
JvdBerg

Reputation: 21856

Use absolute paths!

In your index.php do:

define( 'ROOT', dirname(__FILE__) );

And when you want to include the header file,

include ROOT . '/include/header.php'

And, when in html and want to use a image, use a absolute path from webroot:

<img src="/images/your_image.png">

Upvotes: 2

Related Questions