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.
Related
What is the difference between:
/../../../my_dir/my_file.php
And
../../../my_dir/my_file.php
I was working with a PHP include (couldn't include regularly, i suspect it is because i used namespaces in the file i was including from), the latter didn't work, i randomly added the / before the filepath and now everything is working fine. I am utterly confused since after reading about the functionality of that forward slash, it appears that my code shouldn't work since / refers to the root directory, therefore /../../../ is three levels up the root directory ? What the hell would that even mean ? I really have no idea why my code is working and that file is being included.
So, if / is the root directory, and i am going up three levels up the root, then why does everything work fine ? shouldn't it throw some error ?
Additionally, can the root directory change ? As i understand it, in Windows, my root directory right now is the C: drive, could you change your root from C: to say, a directory in your project ?
Here is a gist if you are interested in taking a look at the file where all this is going on: https://gist.github.com/doubleOrt/09a2b0b97f632143f89e07f21bb974ad
/../../../my_dir/my_file.php
This will change directory based on the root.
./../../../my_dir/my_file.php
This will change the directory, relative to where it's executed
../../../../my_dir/my_file.php
This just goes up up up and up to my_dir/my_file.php
As i understand it, in Windows, my root directory right now is the C: drive, could you change your root from C: to say, a directory in your project ?
Not sure, but you wouldn't want this, as it would be different from your live environment. Root should always be the root.
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.
This seems like a basic question, but it's stumping me. I have CodeIgniter installed, and I've got a model that manipulates and saves XMLs. My problem is when trying to save it to "/" I get a "Permission denied" PHP error. I need to save them to a separate directory relatively, but I'm not sure where exactly "/" is located on the server. Is it in the "/www" of Apache, or root of the whole server? Once I know this I should be able to navigate to the correct directory
/ is the root directory. The starting point of your directory structure. This is where the Linux system begins. Every other file and directory on your system is under the root directory. Usually the root directory contains only sub-directories, so it's a bad idea to store single files directly under root.
Try specifying the complete path in your application.
Example: /home/user/public_html/yourApplicationFolder/
Or specify a relative path:
Example: ../somePath/.
This article could be helpful.
To get the filesystem path to the document root just use $_SERVER["DOCUMENT_ROOT"];
Let’s say I have two folders in my PHP server root directory: x and y. There are two more directories inside the x folder: a and b. In the /x/a directory I have the index.php file and in the /y folder a stuff.php file.
From my /x/a/index.php file, I want to include the /y/stuff.php file. How do I do that?
My server doesn’t allow including files from other domains, so adding the full URL doesn’t work!
Also, I’d like to know how to start a path from the root in PHP. I use ./blabla from the index of my root and ../blabla from directories in the root, but, unfortunately, .../blabla doesn’t work from 2nd grade directories.
To access the root begin your path with "/".
If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../".
If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).
try:
include "../../y/stuff.php";
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');