I recently was watching a php video tutorial and the author was showing how to do include a file. He was using XAMPP for the demonstrations and had many files.
When he was showing how to include a file, he mentioned something about putting two dots (..) in front of the file path (/xampp/content/example.html) because of something having to do with where the files were located, assuming that I already had knowledge of this principle. But i don't.
Can anyone explain what is up with having one dot or two dots in front of file paths?
What is the difference between include("/xampp/content/example.html");, include("./xampp/content/example.html");, and include("../xampp/content/example.html");
In Linux / Unix environment,
/xampp/content/example.html means absolute path
./xampp/content/example.html means relative path of current directory
../xampp/content/example.html means relative path of parent directory
For the folder structure: /var/www/xampp/content/example3.html:
If your current folder is /var/www/...
../ (goes up 1 level) will be /var/
./ (in current level) will be /var/www/
/ will be / (in Linux, / means the root of the server, the outermost structure of the filesystem)
../../ (goes up 2 level) will be /
There are 2 types of paths: Relative Path & Absolute Path.
For Relative path, it's relative to your current directory. For absolute path, it's not related to your current directory.
. means the same directory as the script that's doing the including, .. means the parent directory of the one containing the script. So ../xampp/content/example.html means to go up one folder level from the current script, then go into its xampp/content subdirectory to find example.html.
A path beginning with / is an absolute path from the root of the server. Using absolute paths makes it harder to move your project to a new directory, because you'll need to update all the paths. Relative paths allow you to move everything as a group without changing the paths, because the directory relationships will stay the same.
Related
I searched around but didn't get satisfactory answer.
In directory structure i want to know what is the use of ./
It doesn't impact if I use this in src.
Please let us know difference in ./ ../ and /
./ is the current directory.
../ is the parent directory of current directory.
/ is the root directory of the system.
They work like this:
./ — the current directory
../ — the parent directory of the current directory
/ — the root directory of the file system
./ is the current directory.
../ is the parent directory of current directory.
/ is the root directory of the system.
You can also use __FILE__ and __DIR__ to get the path.
For more details look at this constants predefined
./ means the current directory
../ means the parent of the current directory, not the root directory
/ is the root directory
Explanation
myfile.text is in the current directory, as is ./myfile.text
../myfile.text is one level above you and /myfile.text lives in your
root directory.
If this is about URLs, as it seems – the HTML attribute src takes a URL value – then it really has nothing to with directories. Interpretation of relative URLs takes place as string manipulation, without any reference to directories or files. (A resolved URL, absolute URL, may then be interpreted in a manner that maps things to a file system, but that is a different issue.)
At the start of a URL,
./ has no effect (it is removed in interpreting a relative URL)
../ causes the last part of the base URL, back to its last / in it, to be removed before using it as a prefix
/ causes the URL to be prefixed by the protocol and server part of the current base URL
Thus, assuming a base URL of http://www.example.com/foo/bar/zap,
./test.html resolves to the same as test.html, namely http://www.example.com/foo/bar/test.html
../test.html resolves to http://www.example.com/foo/test.html
/test.html resolves to http://www.example.com/test.html
Reference: STD 66.
If it happens that the absolute URLs are then interpreted, by the server running at www.example.com, in a simplistic (and common) manner by mapping them to a file system, then ./ maps to the same directory as the base URL, ../ maps to its parent directory, and / maps to the server root.
what's the difference between
require("../classes/M8.php");
and
require("/../classes/H8.php");
How will include path will differ?
/.. is a path relative the root of the server.
../ is a path relative to the current script folder.
I see a request for more information is made.
If you are in a folder on your server, say:
/usr/local/apache2/www/example.php
Then
a reference in example.php could be made as an absolute path, so relative to the root of the server: /usr/local/apache2/hidden/credentials.php
Or the same file could be reached using a path relative to the current file: ../hidden/credentials.php. Note that each set of ../ will move you one directory higher in your server folder structure.
A / at the start of a path will point to the root directory. This is known as an absolute path. Anything else is relative to the current folder. This is known as a relative path.
IBM describes the differences here:
An absolute path name represents the complete name of a directory or file from the /(root) directory downward. Regardless of where you are working in the file system, you can always find a directory or file by specifying its absolute path name. Absolute path names start with a slash (/), the symbol representing the root directory. The path name /A/D/9 is the absolute path name for 9. The first slash (/) represents the /(root) directory, which is the starting place for the search. The remainder of the path name directs the search to A, then to D, and finally to 9.
Unlike full path names, relative path names specify a directory or file based on the current working directory. For relative path names, you can use the notation dot dot (..) to move upward in the file system hierarchy. The dot dot (..) represents the parent directory. Because relative path names specify a path starting in the current directory, they do not begin with a slash (/). Relative path names are used to specify the name of a file in the current directory or the path name of a file or directory above or below the level of the current directory in the file system. If D is the current directory, the relative path name for accessing 10 is F/10. However, the absolute path name is always /A/D/F/10. Also, the relative path name for accessing 3 is ../../B/3.
Imagine you're using this structure, for instance, and were working in MyScript.php:
Home
-- Foo
-- Example.php
-- Bar
-- Scripts
-- MyScript.php
If you wanted to include Example.php from within the Foo folder, you could either specify:
../../Foo/Example.php /* Relative path: ..Bar/..Home (Root)/Foo/Example.php */
Or:
/Foo/Example.php /* Absolute path: Home (Root)/Foo.Example.php */
I always worry about pathing issues when I move my website into subfolders because I don't know how to handle it with PHP.
I want to learn this with an example because I can easily learn it by looking at examples.
Let's say I have a website running at this directory: httpdocs/development/subfolder/myWebsite/index.php
By default, PHP would run httpdocs/index.php, so it is the root path but my website runs in a subfolder.
In my website's index.php, (so we're in a subfolder) how can I ensure I point correct folders?
<img src="images/1.jpg"> // Does no dot means root path?
<img src="./images/1.jpg"> //Does ./ means which_directory_our_php_page_currently_in/images?
<a href="./"> // Points /subfolder/myWebsite/ or httpdocs/ ?
<a href=".."> //Same as above, but no front slash.
<a href=""> //Same as above
I don't want to make a definition or a const to track it with PHP and change it whenever I move my files. Like:
define('SITE_PATH', './development/subfolder/myWebsite/');
Especially when it comes into DIRECTORY_SEPARATOR, things only get more confusing.
I would like to know how to handle it with PHP professionally; what is the difference between and ./; lastly what does .. mean without forward slash.
Thank you.
All the paths in your examples are relative, meaning they are based off of the current location. Starting a path with a / means it's an absolute path, based on the root of the site.
If you want to always be 100% sure of what you're referencing use the / at the front of your paths.
In UNIX systems, './dirName' looks for a sub-directory dirName in the current directory; . simply refers to the current location. In other words, ./dirName is equivalent to 'dirName'. You don't use that in an href though: for paths relative to the current location, do not use the preceding ..
'../dirName' looks for a sub-directory dirName in the parent of the current directory. .. refers to the parent directory.
If the filepath begins with a forward slash, it is referring to the root directory. '/dirName' refers to a sub-directory dirName located inside the root directory.
I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com is unnecessary and useless. just /myfolder/ is what you really need.
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)
What is set_include_path Relative to, in PHP? Is it the folder where the PHP.exe resides? Is it the webroot? In other words, what folder would set_include_path('/') or set_include_path('.') be referring to?
Relative paths are resolved from the location of the file where include or another function that uses include_path is used in (see description of include_path):
Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.
/ would describe the root of your filesystem and . the current directory.
The filesystem root and the current directory, respectively.
set_include_path("/") would make the include path be the root folder of the filesystem, and I would take a guess that you'd probably not want to do that as there might be issues with exposing files that you don't want to be seen.
If your php file was /home/users/joebloggs/htmlroot/index.php, then set_include_path(".") would make the include path the directory that the php file is in, ie the "htmlroot" directory.
On *nix systems and Windows Apache the / is the root of the file system. While on IIS / points to the root of the vhost.
What I do to handle this is define a LOC constant in my index.php so I never get confused when including files.
define('LOC', dirname(__FILE__));
include(LOC . '/files/file.php');