What difference in ./ ../ / - php

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.

Related

php include with ".." before file path

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.

Pathing using ./, ../, .. - what do they exactly mean for my view files?

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.

Is there a difference between /../ and ../ in php?

I was curious if ../ and /../ is the same in PHP so I tried these:
require_once('/../frame/header.php');
require_once('../frame/header.php');
Both of them worked. Was I just lucky or they realy do the same exact thing? Why?
In your context if they both worked, that implies that your scripts are at the root of the filesystem you have access to, or one level in. They are not the same however! /../ refers to the filesystem root (and .. one directory up, which just gets eaten and is still the root), while ../ refers to one directory higher than the current one. Any path beginning with / is an absolute path from the filesystem root.
From anywhere other than the filesystem root, these would not function equivalently.
Suppose your working directory is /var/www/scripts.
require_once('../include.php');
Will include a file at /var/www/include.php.
But from that same location, if you did
require_once('/../include.php');
...php will attempt to load the file /include.php at the filesystem root, and it probably won't exist.
Now, a lot of web hosts will supply you with a filesystem whose root / is also the web server's document root, the web server document root is only one directory level in from the root like /www. In that case, /../ may work fine, but beware if you ever attempt to move it to another server with a different filesystem configuration.
So if the script's working directory was /www, just by luck, these two would function the same way:
require_once('../include.php');
require_once('/../include.php');
Both would include the file /include.php at the filesystem root.
Note, this is not PHP-specific, but a property of filesystems which use the .. in general...
Preceding any link with a / mean that you are coming from the root of the context you are in. If you are already in the root directory, or a chroot jail, then both statements are the same.

where does absolute path start? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Absolute path & Relative Path
This is a very basic question, but I have been scratching my head over this for a long time and I am hoping someone can help clear up my confusion.
I have a shared account on a linux server.
The path to public_html is: /home/myusername/public_html
The code for my website lives here: /home/myusername/public_html/mysite.com
Under mysite.com there are directories such as 'mycss', 'myjs', 'myphp', etc.
When I create an absolute path (for example, within php code or an html file), sometimes the path needs to start at /home, eg /home/myusername/public_html/mysite.com/myphp/myfile.php
And sometimes it needs to start inside mysite.com, eg /myphp/myfile.php
My confusion: When does an absolute path need to start at /home, and when does it need to start within mysite.com? Is there a rule of thumb, or some insight anyone can give?
You are confusing two types of absolute / relateive.
IF you are speaking in website terms (images / js / html stuff), your absolute is the path from your root website folder (ie: /home/myusername/public_html/mysite.com)
So if you have a folder like:
/home/myusername/public_html/mysite.com/images/bob.jpg
In your HTML, the absolute path would be /images/bob.jpg, while a relative website path would be images/bob.jpg etc;
IF you are doing PHP / Server side includes, you need to use the FULL absolute path, so your whole /home/myusername/public_html/mysite.com stucture.
But also at the same time, if you want to use a relative method (preferred in most cases as it makes your PHP code easier to move) you would just do an include('../database/connect.php');
It depends on the website. Did you write the PHP code for the website? Usually the absolute path will point to the web root (eg. /home/myusername/public_html/mysite.com ), but if you have config or other included files that are outside of the web root for security purposes - that might require a different path (eg. /home/myusername/notwebroot )
You're confusing what is actually a relative path with an absolute path.
/home/myusername/public_html
is an absolute path
/public_html/anything
/myphp/myfile.php
/mysite.com/anything
these are all relative paths.
If it isn't starting at the root then it isn't an absolute path. If it is starting at the root of your website then it is a relative path because if you move your website to a different folder then the paths to these directories and files don't need to change (i.e. they are relative to the web root).

What does "URL or relative path from IM to your main site" mean?

I wanted to use a script using instant messaging and found this.
The instruction said that to Make sure that in blab_im/config.php you've set properly $site_to_bim and $bim_to_site:
$site_to_bim='localhost/ThesisDB/blab_im/'; // URL or relative path from your main site to BLAB!IM, default:> $site_to_bim='./blab_im/'; [must end > with a trailing slash]
$bim_to_site='../'; // URL or relative path from BLAB!IM to your > main site, default: $bim_to_site='../'; [must end with a > trailing slash]
i was able to understand a little bit the first part(Correct me if I am wrong)
the second part I do not understand which is bim_to_site.
Thanks/Salamat in advance!
The second variable is asking for the path to take from the directory that the im client script is in to the www root.
So, for instance if the im client is here:
www.example.com/files/imclient
that would give you:
$site_to_bim = '/files/imclient/;
$bim_to_site = '../../';
because you go up 2 levels from the im directory to the main site directory.
Update
When you are specifying a relative path, you can start a few different ways:
with a slash: /files/. That will tell the parser (or browser) to try to find the target beginning from the main directory, then into the files directory.
with simply the directory name: files/. That means begin looking where you currently are and look for the files directory. So if the script resides in the imclient directory from the above example, this would mean "look for the files directory inside the imclient directory.
with a dot slash: ./files/. This means the same as the above. Start in the current working directory.
with two dots and a slash: ../files. This means start in the current working directory and move up out of it to the parent directory. So in the example, if the script was in imclient, using ../ would say "look for things in the files directory and ../../ would say "look for things in the main directory (the parent of files).
Does that flesh it out a bit?

Categories