Pathing using ./, ../, .. - what do they exactly mean for my view files? - 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.

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.

Getting the absolute pathname in php

Consider the following scenario: I have a vhost defined to some paths on my home folder.
say ~/web/project-name/ is my root. such that when i point to http://some-name/ it points to the index.php inside ~/web/project-name.
I've a Model-View-Controller framework (self-made/minimal) and my views contains different client side links (js, or css, or a href) Since I made my working folder root, i used absoulte path names (for instance /client/css/my.css ).
Now a friends comes in takes my projects. Copies it to /var/www/ So, now the contents of my website is not root, so my links in the views does not work?
What is the best way to mitigate the above problem?
I tried defining a constant ROOT as define('ROOT', dirname(__FILE__)) in my index.php, but it returns the absolute path like /home/cipher/...
I want to make a function such that it returns the path of my index.php relative to the web root!
Thanks in advance!
You might want to try a $_SERVER['DOCUMENT_ROOT'].

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.

include works only when file is in same directory

I have this site I'm making. When I include a php file from the same folder it works but when I do
<?php include('/ioanblog/appstore/header.php');?>
it doesn't work or
<?php include('http://www.domain.co.uk/appstore/header.php');?>
that isn't working either.
All the header is holding is a style sheet and Piwik code, it will also hold navigation.
It's hard to say why the include won't work without knowing the folder structure.
But you can try it with the absolute path: /home/user/domain/public_html/etc...
Read the documentation on relative paths.
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
You are using /ioanblog/… which is an absolute path. I suspect your site is in /var/www/public_html/ioanblog/… or /home/user/ioanblog/… or something similar, not in /ioanblog/…. You should remove the first / making it a relative instead of an absolute path. You might have to go "up" to parent directories like ../contents/header.php or ../../contents/header.php for the actual file.
As pointed out by brbcoding it should probably be ../../header.php. But first make sure you understand absolute/relative paths!
You might also want to read up on realpath() to 'convert' relative paths to absolute paths.
Use <?php include('../../header.php');?>
That's my guess... You don't have an appstore directory anywhere as far as I can tell.
EDIT: Updated with the correct path... Coming from the libreoffice directory.
try removing the first slash so include('ioanblog/appstore/header.php');

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