This Website has been Deprecated. Use
For Prework and Written Curriculum

Relative Path using PHP

The following method will ensure that all of our LOCAL linked resources in our head-utils.php file will load properly on every page of our site. This code will create a variable $PREFIX which will be used to automatically resolve the correct directory path sitewide. For example, you will use this method when loading internal CSS files in your head-utils.php file.

This does not apply to assets you would load using require_once().


Review the source code in the panels below.

  1. Add the file root-path.php to the active root of your project. (This should either be the true project root, or /public_html)
  2. Add the code inside of head-utils.php to the very top of your head-utils.php file.
  3. Add the code inside of index.php to the top of EACH web page.
  4. Use the $PREFIX variable when linking to an internal asset, such as a CSS file, in your head-utils.php file. See examples below.


 * defines the root path of the entire site
 * @see FarmToYou Definition



 * Get the relative path.
 * @see FarmToYou Header

// include the appropriate number of dirname() functions
// on line 8 to correctly resolve your directory's path
require_once(dirname(dirname(__DIR__)) . "/root-path.php");
$CURRENT_DEPTH = substr_count($CURRENT_DIR, "/");
$ROOT_DEPTH = substr_count($ROOT_PATH, "/");
$PREFIX = str_repeat("../", $DEPTH_DIFFERENCE);


/*grab current directory*/
/*set page title here*/
/*load head-utils.php*/


Echo the $PREFIX variable when linking/loading internal assets in your head-utils.php file:

<link rel="stylesheet" href="<?php echo $PREFIX; ?>lib/css/styles.css" type="text/css" />

Ensure that each page can have it's own unique title text when using a head-utils.php file:

<title><?php echo $PAGE_TITLE; ?></title>