what does /../ or "/../" mean in file system? - php

I know that . refers to current directory and ./ refers to files in current directory.
I also know that .. refers to parent directory
Recently i came across the following line:
require __DIR__.'/../vendor/autoload.php';
(It was in laravel's index.php file.)
Here specifically what is meant by "/../"?
Thanks
Edit : I tried searching on google. I tried searching using escape sequences too like //..//, but it didn't give required answer.

You read it like this:
__DIR__ // start in the current directory
../ // go up one directory from where you are
vendor/ // go into the vendor directory
autoload.php // and require the autoload.php file
The extra / between __DIR__ and ../ is just to ensure the current directory ends in a slash, so you get, for instance code/../vendor/autoload.php instead of code../vendor/autoload.php

In paths . referes to the current (working-)directory and .. referes to parent directory (if exist). Both are "shortcuts" for actual writing the named of those directories (If for example you don't know the name of the current/parent directory): Here is an example:
If you are in folder /root:
1 ./test //--> points to folder 'test' (relative path)
2 /root/test //--> points to folder 'test' (absolut path)
both are equal. And if you are in folder /root/test:
1 ./.. //--> points to folder 'root' (relative path)
2 /root //--> points to folder 'root' (absolut path)

../ go up to the parent directory and then be in the same root director of the project / before ../
require __DIR__.'/../vendor/autoload.php';

Basically, /../ doesn't have any specific definition because it is the part of __DIR__.'/../vendor/autoload.php' which consisted of .. and /vendor/autoload.php. Which .. means go back to current parent directory and the later tells you to enter or cd to vendor directory then proceed to autoload.php, and everything relative to __DIR__.
Example:
Imagine you want to gedit a file in another directory from your current directory which structured below
/home/handsomeyou/Documents/Code/ -> your current directory
/home/hansdomeyou/Documents/Tasks/task_list.txt -> your target
There are many approach to achieve this, but to make it relevant to your question, we will do this
$ gedit ../Tasks/task_list.txt

It referees to the parent directory of the current path (of curse if the parent directory exists).

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.

What difference in ./ ../ /

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.

PHP: Why isn't my `set_include_path()` working when pointing up one directory?

There's a little library I'm trying to plug in to my project. This lib has some includes and requires across classes, so I'm trying to set an include path for all that to work.
When trying to set an include path off a sibling branch, I run into a hiccup.
For a reference point, require('/../my/test.php') works fine.
So does
set_include_path('/../');
require_once('my/test.php');
But once I try
set_include_path('/../my/');
require_once('test.php');
I get:
Warning: require_once(one.php): failed to open stream: No such file or directory in ...
What am I missing?
Starting your paths with / means look in the root directory, so /../ is technically one directory above the root directory.
To set the include path to the parent directory of the current location, you just need ../. To make the code more portable I would suggest combining it with dirname(__FILE__) to get the absolute path of the current directory.
IE:
set_include_path(dirname(__FILE__) . '/../my/');
Note the preceding / is required in that example as dirname() does not return a trailing slash

Finding Directory Name in other directory with PHP

I have a file which name is load.php near index.php in root folder. So i only have two files in root. I like clear works like most.
Today i needed to define directories in one file name is define.php. But it is not in root, i've added it to settings folder in root folder.
So my files:
index.php (requires load.php)
load.php (requires settings/define.php)
settings (includes define.php)
Now i don't know how to get root folders name?
__DIR__ in define.php gives me .../home/settings folder but i want to get root or another folders name. So what is the way?
To find out the "root" directory from inside settings/define.php, this should be sufficient:
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..');
See it in action.
Note: the linked example uses dirname(__FILE__) instead of __DIR__ because the latter is only available on PHP >= 5.3, but is functionally equivalent.

Requiring files from the root directory

Is it possible to require files (using php require) from the directory where my website is placed?
For example, my website is in the directory mywebsite which is in the root directory. There is another directory there. Can I require files from this another directory?
Sure, you can require files from anywhere that has the appropriate permissions.
This requires the file from the current directory (NOT always where the current PHP script is, so be careful of that):
require("whatever.php");
This will require whatever.php from somefolder which is in the current directory.
require("somefolder/whatever.php");
Finally, you can give an absolute path like this:
require("/var/www/includes/whatever.php");
Require from parent directory:
require("../includes/watherver.php");
It doesn't matter really where you get it from, provided you have the permissions set correctly, and PHP is configured in such a way to allow you to do so.
I have been using
require_once $_SERVER['DOCUMENT_ROOT'] . '/myfile.php';
It should be no problem to require from an arbitrary existing and readable directory.
Image you have:
/
--folder1
--folder2
and in folder1 is your index.php and in folder2 is your to_require.php
Then you could write:
require('../folder2/to_require.php')
That's because you can go up in your directory tree with ..

Categories