PHP include "../" vs "/../" - php

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 */

Related

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

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).

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.

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.

PHP root folder

rename('/images/old_name.jpg', '/images/new_name.jpg');
This code gives file not found.
Script, where files are called is placed inside /source/ folder.
Files can be opened from http://site.com/images/old_name.jpg
How to get these files from root?
rename is a filesystem function and requires filesystem paths. But it seems that you’re using URI paths.
You can use $_SERVER['DOCUMENT_ROOT'] to prepend the path to the document root:
rename($_SERVER['DOCUMENT_ROOT'].'/images/old_name.jpg', $_SERVER['DOCUMENT_ROOT'].'/images/new_name.jpg');
Or for more flexibility, use dirname on the path to the current file __FILE__:
rename(dirname(__FILE__).'/images/old_name.jpg', dirname(__FILE__).'/images/new_name.jpg');
Or use relative paths. As you’re in the /script folder, .. walks one directory level up:
rename('../images/old_name.jpg', '../images/new_name.jpg');
In PHP the root (/) is the root of the filesystem not the "webroot". If the php-file is in the /source/ directory and images are in /source/images/ then this will work:
rename('images/old_name.jpg', 'images/new_name.jpg');

Categories