I'm not sure to really understant the fundamental difference between this two ways to access a path :
"/"
"./"
Can somebody help my to find it out ?
the dot just refers to the current directory. but there are special meanings to some
1) . - current directory
2) .. - the directory before this current directory
3) ./somefolder - refers to a subfolder called 'somefolder' in this directory
4) ../hello - refers to a folder in a root directory with reference to ur current directory called 'hello'
Related
In a file path, the set of "words" or folder names required to specify a specific file in a hierarchy of directories is called the path to the file, which is called path name. Path name can be either absolute or relative. In relative form, a sort of patterns like "./", "../", "../../", ... can be used to show the file/folder depth corresponding to the project base folder.
I know what they do or implying by behavior, but what are they called? Do they have a specific name? For example, what is called "../" or "..\" in a file system?
Briefly, it is called levelup notion.
Let's start with defining path:
A path is a slash-separated list of directory names followed by either
a directory name or a file name. A directory is the same as a folder.
Then, we look at types of paths: Absolute and relative paths!
An absolute, or full, path begins with a drive letter, whereas a relative path refers to a location that is relative to a current directory.
In relative paths we may use dot and double-dot symbols. A dot here means the current directory itself. And the double-dot notation, which is called level-up, is used for moving up in the hierarchy.
Now to change the directory using the dot notion we do as follows:
A single dot represents the current directory itself. for example:
in a command line interface (e.g. PowerShell):
cd . will not change the directory as itself point to the current directory.
To change the directory using the level-up notion we do as follows:
each double-dot notions takes you one level up, meaning one folder closer to the base drive or root folder in the hierarchy.
Look at the example below for different cases of using levelup notions:
$link= PS_ADMIN_DIR;
$admin_folder = substr(strrchr($link, "\ "), 1);
currently i am using this way to get folder name,
But if there are any direct method or any constant please suggest me..
Thanks
To be a little bit more specific : the name of the admin directory is on the filesystem.
When you access a page of the admin directory, a script puts the current directory's path in the _PS_ADMIN_DIR_ constant.
If you forgot the name of the the admin directory you must have a look at the filesystem of your server.
Admin directories are automatically renamed to something like adminXXXX.
If you named it differently you can compare the default directory structure with your actual structure and find the proper directory.
You can also look for files that are only present in the admin directory. The "get-file-admin.php" file for example.
On linux, the following command run from the prestashop root directory will tell you the actual name of the admin directory :
find ./ -name get-file-admin.php
For security reasons, admin folder name is not stored anywhere in your PrestaShop's files or database, so you have to do something like you do to find it.
However, you should use _PS_ADMIN_DIR_ instead of PS_ADMIN_DIR as the second one is not defined directly by PrestaShop and could be undefined.
How do I get application's root directory within an action?
The first thing ZF2 does is to change the current dir via chdir(dirname(__DIR__));
This means that every future include is based off of the ROOT PATH of your application and NOT the public folder. Or any other current folder.
Of course this only holds true for PHP-Files.
If you want to define the root path manually, you'd go to /public/index.php and add a line like define('ROOT_PATH', dirname(__DIR__));
As i said before, for INCLUDES this is NOT required though ;) as you're ALWAYS in the root folder when it comes to PHP-Files ;)
getcwd() works best for me, DIR return the module root. Which isn't much use in this case
#Sam:
I don't really understand your question. Basically the current path equals the ZF2-Apps Root. [...] You can always go up-levels, too via ../
Not exactly. When You create module shared within several applications ex. FileUpload Module in vendor, outside application. You would like to upload file to Application subdirectory not shared module :) In this case __DIR__ equals module path not app path and ../ wouldn,t be good solution ;)
I like ROOT_PATH as You have mentioned:
define('ROOT_PATH', dirname(__DIR__));
or even better:
getcwd()
In a question regarding a jQuery Ajax problem, the asker was trying to use a . in the beginning of a relative URL. I advised him to remove it, but have no idea what a dot actually does there.
His relative URL looked like this:
./delete-misc/test-ajax-code.php
I tried looking in the RFCs, without success. I know what the dot does in command line (either Linux or Win), it represents the current directory.
I'd like to know: how does this work on the Internet in a URL? Does it have any best-practice uses? Detailed explanations are most welcome.
The path segment . is typically used at the begin of relative path references and are removed during the reference resolution, i.e. the process of resolving a relative URI reference to an absolute URI:
The path segments "." and "..", also known as dot-segments, are
defined for relative reference within the path name hierarchy. They
are intended for use at the beginning of a relative-path reference
(Section 4.2) to indicate relative position within the hierarchical
tree of names. This is similar to their role within some operating
systems' file directory structures to indicate the current directory
and parent directory, respectively. However, unlike in a file
system, these dot-segments are only interpreted within the URI path
hierarchy and are removed as part of the resolution process (Section
5.2).
There is Remove Dot Segments algorithms that describes how these dot segments are to be interpreted in a certain base path context.
In your case both ./delete-misc/test-ajax-code.php and delete-misc/test-ajax-code.php are equivalent. But there are cases where a relative path can be misinterpreted as an absolute URI, e.g. having a : in the first path segment like search:foo that is different to ./search:foo as the former is an absolute URI while the latter is a relative URI path.
A ./ in front of the URL is equivalent to the current path. So ./delete-misc/test-ajax-code.php and delete-misc/text-ajax-code.php are both relative paths. In the answer you posted, you asked to remove the dot only, so the path of /delete-misc/test-ajax-code.php would translate as an absolute path instead of a relative path.
Edit: one more thing - . is the current directory and .. is the parent directory. As phihag comments, these really should be avoided and protected against in code. Directory traversal can be used for evil.
Now for a Simpler Explanation...
. and .. are NOT equivalent!
./ and ../ are NOT equivalent!
. and ./ ARE equivalent to
In all cases, . and ./ are the same as or "" or no path so not needed or used on the Web.
. (dot) is a relic of old UNIX pathing systems and NOT used on the World Wide Web for creating paths! Why? Because the dot in paths is redundant and equivalent to "" or no path or the current file directory you are in. The same result applies to using ./. It is the same as "" or no path. Both just reference the local directory your file is in ("./webpage.html" = "webpage.html").
What Path Should I Use?
So NEVER use . or ./ as both paths are irrelevant!
ALWAYS use ../ which is a RELATIVE PATH and says go up one folder.
ALWAYS use / which is an ABSOLUTE PATH and says starts from the website root folder.
Want More Proof?
Check out the result for these image paths. Assume you are referencing these paths from an HTML web page stored in the root of the web site:
SUCCESSFUL PATHS ("../" and "/" paths work well on the Web)
<img src="../images/photo.jpg" />
<img src="/images/photo.jpg" />
REDUNDANT PATHS ("." not needed)
<img src="/images/./photo.jpg" />
...same as...
<img src="/images/photo.jpg" />
<img src="/images./photo.jpg" />
...same as...
<img src="/images/photo.jpg" />
FAILED PATHS ("." in paths that fail on the Web)
<img src="/images/.photo.jpg" />
<img src="./images/photo.jpg" />
What is the difference between:
include("./somepath/class.php");
and
include("somepath/class.php");
There shouldn't be any difference, directory wise, since the former assumes relative directory structure. The only potential pitfall could be if "somepath" were actually a command to be run - some users expect to type a command for a local script file and assume it should run, when you actually have to run "./somepath" to invoke it. This, of course, only pertains to commands not on your $PATH.
I don't see any difference. "." means current directory.
. refers to the current working directory.
Sometimes you want to specify explicitly that what you want is in the current working directory, and that it is not from something in your path variable for example.
For example in PHP "somepath/somefile" is appended after paths specified in include_dir (for example: /home/var/; /home/bin/ ....) directive.
The second variant is more specific it says: search in current directory!