Skip to content

Instantly share code, notes, and snippets.

@earth3300
Last active March 6, 2020 19:01
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 earth3300/104afdab0e69ee8ce909e9ccb80d63b1 to your computer and use it in GitHub Desktop.
Save earth3300/104afdab0e69ee8ce909e9ccb80d63b1 to your computer and use it in GitHub Desktop.
Get the Paths Used in the Current Project
/**
* Get the Paths Used in the Current Project
*
* Uses options values if these are set. Modify on a per-project basis.
* Intended to be used to save and retrieve files, but can be used more
* generically as it sets and standardizes the document root and the relative
* path to the directory in which the containing file resides.
*
* Last updated: 2020-03-06
* No. of lines: 42
*
* @param array $file
*
* @return array
*/
private function getPaths( $file )
{
// The document root is the highest level accessible by the domain.
$file['path']['root'] = rtrim( $_SERVER['DOCUMENT_ROOT'], '/' );
// The path to the directory this file is in.
$file['path']['dir'] = __DIR__;
// The difference between the root path and this directory (relative path).
$file['path']['rel'] = str_replace(
$file['path']['root'], '', $file['path']['dir'] );
// Used only if source files are needed.
$file['path']['in'] = null;
// Set the directory out explictly (from the root). Can be empty.
$file['dir']['out'] = $this->opts['dir']['out'];
// Set the path out explicitly.
$file['path']['out'] = $file['path']['root'];
$file['path']['out'] .= $file['path']['rel'];
// Set the file out explicitly (from the options).
$file['file']['out'] = $this->opts['file']['out'];
// Find the file name (no slashes).
$file['file']['name'] = ltrim( $this->opts['file']['out'], '/' );
// Set the path to the file out (Use only if it does not interfere).
$file['path']['file']['out'] = null;
// Set the path to the backup (with date appended).
$file['path']['file']['backup'] = $file['path']['out'];
$file['path']['file']['backup'] .= $this->opts['dir']['backup'];
$file['path']['file']['backup'] .= $file['file']['out'];
$file['path']['file']['backup'] .= '-' . date( 'Y-m-d' );
// Set the path to the file in the document root explicitly.
$file['path']['file']['root'] = $file['path']['root'];
$file['path']['file']['root'] .= $file['file']['out'];
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment