- How do you define paths in application?
- 3 Answers 3
- Absolute File Path
- Absolute & Relative Paths In PHP (A Simple Guide)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- PHP FILE PATH
- 1) ABSOLUTE & RELATIVE PATH
- 2) CURRENT WORKING DIRECTORY (CWD)
- 3) CONFUSION WITH THE CURRENT WORKING DIRECTORY
- 4) CHANGING THE WORKING DIRECTORY
- 5) MAGIC CONSTANTS
- 6) MAGIC CONSTANTS VS WORKING DIRECTORY
- 7) MORE PATH YOGA
- SERVER SUPERGLOBAL
- PATH INFO
- BASENAME
- DIRNAME
- REALPATH
- DOWNLOAD & NOTES
- SUPPORT
- EXAMPLE CODE DOWNLOAD
- EXTRA BITS & LINKS
- EXTRA) FORWARD OR BACKWARD SLASH?
- EXTRA) CASE SENSITIVE
- ALL THE PATH-RELATED VARIABLES
- ALL THE PATH-RELATED FUNCTIONS
- LINKS & REFERENCES
- INFOGRAPHICS CHEAT SHEET
- THE END
How do you define paths in application?
Also i want to ask if there is better way to do this?
3 Answers 3
You mean your application is not public? Anyway, normally I just define a ROOT constant in my front controller (usually index.php ) like this:
define('ROOT', str_replace('\\', '/', __DIR__));
Or on older versions of PHP where __DIR__ is not available:
define('ROOT', str_replace('\\', '/', dirname(__FILE__)));
Since the inner structure never changes I just do something like:
include(ROOT . '/application/libraries/Email.php');
define('LIBRARY_PATH', ROOT . '/application/libraries'); include(LIBRARY_PATH . '/Email.php');
In my case PUBLIC_PATH is the same as you described: str_replace(‘\\’, ‘/’, __DIR__ ) application dir is intended to be outside web root (security measure) so it needs additional constant: APPLICATION_PATH
According to your directory tree:
This is the one I would use to LOAD PHP script, basically you can place it in index.php or in bootstrap.php
define("PROJECT_DISK_PATH", str_replace('\\', '/', dirname(dirname(__FILE__))) . '/'); /* Server variables $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_FILE_NAME'] are both USELESS to accomplish this task because they both return the currently executed script and not this included file path. */
Then in your PHP script you do:
include(PROJECT_DISK_PATH . 'path/to/your/script/somescript.php')
And these are the ones I would use to LOAD JS/CSS script IN PAGES:
define("PROJECT_DOCROOT_PATH", '/' . substr(PROJECT_DISK_PATH, strlen($_SERVER['DOCUMENT_ROOT'] . '/'))); define("PROJECT_HTTP_PATH", "http://" . $_SERVER['HTTP_HOST'] . JPL_DOCROOT_PATH);
So in your page you can do:
Absolute File Path
This problem is still not solved A video PHP tutorial I am following is building a file called initialize.php in which it is using the PHP pre-defined constant Directory_Separator and then defining a site_root. The site_root is the absolute file path (not the webserver path) for PHP to locate the files it needs. He gave us the following code
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');
I am assuming the file path on his computer is root/users/kevin/sites/photogallery I am not building the site on my computer, but rather directly online. I don’t know what file path to insert instead. As he emphasized that it’s not the webserver path, but rather the file system path, what do I put instead. Just the domain name like this.
define('SITE_ROOT', DS. 'www.example.com');
He doesn’t want the webserver path but the files are located online? so I don’t get it. UPDATE The video tutorial used the following code
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery'); defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__))); defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');
require_once(/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php): failed to open stream: No such file or directory in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11 Fatal error: require_once(): Failed opening required ‘/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php’ (include_path=’.:/hsphere/shared/apache/libexec/php5ext/php/’) in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11
define('SITE_ROOT', DS.'hsphere'.DS.'local'.DS.'home'.DS.'c263430'.DS.'quoralist.com');
Warning: require_once(LIB_PATH/config.php): failed to open stream: No such file or directory in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11 Fatal error: require_once(): Failed opening required ‘LIB_PATH/config.php’ (include_path=’.:/hsphere/shared/apache/libexec/php5ext/php/’) in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11
Absolute & Relative Paths In PHP (A Simple Guide)
Welcome to a quick tutorial on absolute and relative paths in PHP. So you have set a verified file path, but PHP is still complaining about “missing” files and folders? Yes, it is easy to get lost in absolute and relative paths in PHP.
- An absolute path refers to defining the full exact file path, for example, D:\http\project\lib\file.php .
- While a relative path is based on the current working directory, where the script is located. For example, when we require «html/top.html» in D:\http\page.php , it will resolve to D:\http\html\top.html .
The covers the basics, but let us walk through more on how file paths work in PHP – Read on!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
TABLE OF CONTENTS
PHP FILE PATH
All right, let us now get into the examples of how file paths work in PHP.
1) ABSOLUTE & RELATIVE PATH
- An absolute file path is simply the “full file path”. Although a bit long-winded, it’s hard to mess up with this one.
- On the other hand, the relative file path is based on the current working directory.
But just what the heck is the “current working directory”? Follow up with the next example.
2) CURRENT WORKING DIRECTORY (CWD)
In simple terms, the current working directory is the folder where the script is placed in. We can easily get the current working directory with the getcwd() function.
3) CONFUSION WITH THE CURRENT WORKING DIRECTORY
// (A) CWD IS BASED ON THE FIRST SCRIPT! // if this script is placed at "D:\http\inside\3-cwd.php" // cwd is "D:\http\inside" require "D:\\http\\2-cwd.php";
So far so good with relative paths? Now comes the part that destroyed many beginners, take note that this example script is placed at D:\http\inside . Guess what getcwd() shows when we run this example? Yes, the current working directory now changes to D:\http\inside . Keep in mind, the current working directory is fixed to the first script that runs .
4) CHANGING THE WORKING DIRECTORY
The current working directory will surely mess up a lot of relative paths. So, how do we fix this problem? Thankfully, we can use the chdir() function to change the current working directory.
5) MAGIC CONSTANTS
"; // (B) CURRENT FOLDER // if this file is placed at "D:\http\5-magic.php" // __DIR__ is "D:\http" echo __DIR__ . "
";
- __FILE__ is the full path and file name where the current script is located.
- __DIR__ is the folder where the current script is located.
6) MAGIC CONSTANTS VS WORKING DIRECTORY
"; // (B) CURRENT FOLDER // if this file is placed at "D:\http\inside\6-magic.php" // __DIR__ is "D:\http\inside" echo __DIR__ . "
"; // (C) GET PARENT FOLDER $parent = dirname(__DIR__) . DIRECTORY_SEPARATOR; require $parent . "5-magic.php";
- The current working directory is based on the first script that we run.
- Magic constants are based on where the scripts are placed in .
Yes, magic constants are the better way to do “pathfinding”, and to build absolute paths.
7) MORE PATH YOGA
"; // D:\http echo $_SERVER["PHP_SELF"] . "
"; // 7-extra.php echo $_SERVER["SCRIPT_FILENAME"] . "
"; // D:/http/7-extra.php // (B) PATH INFO $parts = pathinfo("D:\\http\inside\\index.php"); echo $parts["dirname"] . "
"; // D:\http\inside echo $parts["basename"] . "
"; // index.php echo $parts["filename"] . "
"; // index echo $parts["extension"] . "
"; // php // (C) BASENAME $path = "D:\\http\\inside"; echo basename($path) . "
"; // inside $path = "D:\\http\\inside\\foo.php"; echo basename($path) . "
"; // foo.php $path = "D:\\http\\inside\\foo.php"; echo basename($path, ".php") . "
"; // foo // (D) DIRNAME $path = "D:\\http\\inside\\"; echo dirname($path) . "
"; // D:\http echo dirname($path, 2) . "
"; // D:\ // (E) REALPATH echo realpath("") . "
"; // D:\http echo realpath("../") . "
"; // D:\
Finally, this is a quick crash course on the various variables and functions that may help you find the file path.
SERVER SUPERGLOBAL
- $_SERVER[«DOCUMENT_ROOT»] – Contains the root HTTP folder.
- $_SERVER[«PHP_SELF»] – The relative path to the script.
- $_SERVER[«SCRIPT_FILENAME»] – The full path to the script.
PATH INFO
- dirname – The directory of the given path.
- basename – The filename with extension.
- filename – Filename, without extension.
- extension – File extension.
BASENAME
The basename() function will give you the trailing directory or file of a given path.
DIRNAME
The dirname() function will give you the parent directory of a given path.
REALPATH
The realpath() function gives you a canonicalized absolute path… Kind of useful, but still based on the current working directory.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
EXTRA) FORWARD OR BACKWARD SLASH?
Not really a big problem though, just use DIRECTORY_SEPARATOR in PHP and it will automatically resolve to the “correct slash”.
EXTRA) CASE SENSITIVE
- We have a Foo.php script.
- Windows is not case-sensitive – require «FOO.PHP» will work.
- But Mac/Linux is case-sensitive – require «FOO.PHP» will throw a “file not found” error.
ALL THE PATH-RELATED VARIABLES
ALL THE PATH-RELATED FUNCTIONS
- dirname – The directory of the given path.
- basename – The filename with extension.
- filename – Filename, without extension.
- extension – File extension.
LINKS & REFERENCES
INFOGRAPHICS CHEAT SHEET
THE END
Thank you for reading, and we have come to the end of this guide. I hope it has helped you find the true path, and if you have anything to add to this guide, please feel free to comment below. Good luck and happy coding!