Skip to content

Instantly share code, notes, and snippets.

@u01jmg3
Created June 10, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u01jmg3/5351da8e568032a158b045f287e402bc to your computer and use it in GitHub Desktop.
Save u01jmg3/5351da8e568032a158b045f287e402bc to your computer and use it in GitHub Desktop.
Generate an absolute or relative path based on the location of the current file
<?php
// Absolute
$path = current(preg_split('@(?<=public)@', getcwd()));
require_once "{$path}\includes\config.inc.php";
// Relative
$path = preg_split('/public/', getcwd());
$path = explode(DIRECTORY_SEPARATOR, trim(end($path), DIRECTORY_SEPARATOR));
require_once str_repeat('../', count($path)) . 'includes/config.inc.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment