0

I am a serious low-level PHP user, I get the basics up to a point, and I am using includes on my website. I am almost sure that what I asking is possible, but have no clue how to do it.

I have a nested folder in my website for my e-store, so my php include files inside the store are set to use ../inc/filename but the local refs in those files are not being found, because they each have to be leveled up, also. This means I would have to create a separate includes directory just for the store and any changes to the main includes would also need to done in the store includes as well, creating double the work. I would prefer to pass on that option and find away to use a single include directory for everything on my website.

Also, since it is a photo site, I have hundreds of images already in directories. I would very much like to not have to use '../' for each image, it jumbles up the code, I prefer clean code.

I am trying to find a way to use PHP to DEFINE or SET or search or whatever to get them to automatically find the correct directories and not have to worry about leveling up everything. This way, the include files would be found and all the local href links will still work and all 'img' tag will find their correct directories without using '../img/filename' etc.

I am aware that live, I can just use /inc/filename or /img/filename, but locally on MAMP I get errors & it does not make for a good localhost development setup.

If there is an alternate way, I am open to all suggestions for clean & efficient way to make this happen. If you don't mind, please provide examples of how it works, I am very much a visible learning person.

Test site: Photo Store. I am constantly updating this so it maybe in a different state by the time you get to it, being very early in development and all, although the main site is complete.

Sample of my PHP page headers, this is from my main shop page the store directory. Notice I already created a separate include directory inside the store directory, but would prefer to just reference the main include directory one level up, provided examples of both.

`<?php
require('../../photoconfig.php');
ob_start();

$page_name = "Shop";
$page_id = "shop";

//using main include directory with level up as one option
include_once '../inc/top.php'; //doctype to head close
include_once '../inc/header-nav.php'; //body thru nav close

//using store include directory as second option
include_once 'str_inc/top.php'; //doctype to head close
include_once 'str_inc/header-nav.php'; //body thru nav close

 error_reporting(E_ALL);  // error reporting only used in development
?>`

Thank you so very much and have a great day!

Lisa Rose

LisaRose
  • 11
  • 3

1 Answers1

0

$_SERVER['DOCUMENT_ROOT'] should be what you're looking for (like here). The $_SERVER variable is a PHP-created global variable similar to $_POST or $_GET (except that it's always populated).

Example:

<?php
require($_SERVER['DOCUMENT_ROOT'] . '/photoconfig.php');
ob_start();
Kaboodleschmitt
  • 453
  • 3
  • 13
  • Note: `$_POST` and `$_GET` are also "always there" but if they have no params they return empty arrays. – Cornel Raiu Aug 21 '20 at 00:59
  • Thank you! I would like to expand a little on my question, and regrettably, I failed to include this in my original question. This is my current config file. Does this effect what I am trying to do? ` //set reference to root folders define('SITE_ROOT', dirname($_SERVER['PHP_SELF'])); //set image folder path define('IMG_DIR', SITE_ROOT . '/i/'); ` Sorry, I don't know why I didn't even think about adding what was in there. It was a template I got in school & I rarely ever change it. Upon further reading, I think this is where I should make changes, correct? Thanks! – LisaRose Aug 21 '20 at 16:19
  • You're on the right track with that, though I think `$_SERVER['DOCUMENT_ROOT']` is actually better-suited for your needs than `$_SERVER['PHP_SELF']`. If you have that in a config file and the config file is loaded in each PHP file you need to make these calls, you can simply do something like this: `require(SITE_ROOT . '/photoconfig.php');` for most things and something like `` for your images – Kaboodleschmitt Aug 21 '20 at 16:24