PHP The difference between complete also relative paths. The file system is also on the site.


Your website exists in two dimensions.
Real and virtual.

For all visitors, this is a virtual web server. Which differs, among other things, by the fact that it DOES NOT EXIST FILES. If you write <a href="http://site.ru/file.html" target="_blank">http://site.ru/file.html - this is not a file. This is a URI, a virtual address. There can be no file named file.html on the server at all. These are all virtual addresses, not files.
And the browser works with addresses.

For a developer, the same site is a program running on a completely specific real computer. With a completely specific hard drive, directories and files. And the script, working with its data, loading other scripts, works with real FILES, on a physical DISK.

That's the difference and the difficulties often encountered by beginners.
Lose files, confuse links with files, access local files via HTTP, or include files from the root of the web server.

And only two things must be clearly understood:
1. Distinguish the root of the web server, as the browser sees it, and the root of the file system on the disk.
2. The difference between relative paths and absolute.

Let's start with the second one.
It's very simple. If the path is indicated from the root of the system, then this path is absolute. It's like a postal address in real life - from where you would not go, but at the exact address you always will definitely find the right place.
Examples of absolute paths:
/var/www/site/forum/index.php
/img/frame.gif
с:\windows\command.com
/var/www/site/forum/index.php
/img/frame.gif
с:\windows\command.com
In /var/www/site/forum/index.php
/img/frame.gif
с:\windows\command.com
systems and on Web sites, the root is denoted by a slash - "/".
It is important. This is not just a wand, but an independent ADDRESS, the way.
In the address http://www.site.ru/ last slash is not for beauty! It stands for a very specific address - the beginning of the site.
On the disk in Unix systems, you can also type "cd /" and you will be taken to the root directory.
In Windows systems, the file system is broken up by disks, therefore, in the absolute address it is necessary to specify the disk name. The absolute root of the entire file system is not in Windows, each disk has its own. For example, C: \ E: \
Therefore, even if the path to the Windows starts with a slash, then this is not an absolute path, but a relative one. Regarding the current disc. And the absolute begins with a letter.

If you do not specify a root at the beginning of the path, then this path will be relative, and it extends from the current position. In real life, this is reminiscent of the road to the wine store - "two blocks to the left and there all the time straight." You can reach this path only from a specific point. From another you will get to a completely different place.
The simplest example of a relative path is simply the filename.
If the file is in the same directory as the program is running, it will find it by adding the current path to the file name.
Examples of relative paths:
file.php (the file is in the same folder)
./file.php (the file is in the same folder, it is sometimes required in some Unix systems)
images/picture.jpg (the file is in the image file, which is in the current one)
../file.php (the file is in a folder that is one level up from the current one)
../../file.php (the file is in a folder that is located two levels up from the current one)

Both the operating system and the browser, having met the relative path, finish it up to the absolute. But each in his own way.

Now let's proceed to the first point.
The difference between the root of the web server, as seen by the browser, and the root of the file system on the disk.
In general, from the previous explanations, everything should be clear.
On the disk, the path to the script file can be:
/var/www/site/forum/index.php At the same time, the virtual address of this script when browsing through the browser will be:
http://www.site.ru/forum/index.php In this example, it's easy to see where two dimensions intersect: these two addresses have a common part - /forum/index.php - and it /forum/index.php .
For the browser, this is the fullest path that only can be. It starts from the root of the site.
For a script that is running on the server - it's just a part of the path.
For the script the path /forum/index.php turns out to be nonexistent - there is no forum directory in the root of the disk!
To get the full path for what looks like /forum/index.php on the site, you need to put the path to the folder on the left, which is considered the root for the entire web server.
In our example, this is
/var/www/site Var /var/www/site This path is specified in the configuration of the web server and it is contained in the PHP system variable $_SERVER [ 'DOCUMENT_ROOT' ] $_SERVER [ 'DOCUMENT_ROOT' ]

In the same virtual server - the volume that the user sees - on the contrary, there is no disk. There is a root of the site. That is, to ensure that any link is guaranteed to work, regardless of where it is called from, it should be absolute.
If you have on the site, for example, two sections:
http://www.site.ru/about/info.php
and
http://www.site.ru/job/vacancy.php
Then, if you just link vacancy.php to vacancy.php , then the browser will not find it - it will look for the address http://www.site.ru/about/vacancy.php , completing the path from the current directory.
/job/vacancy.php , you must write the full path from the root of the site - /job/vacancy.php
All this concerns, of course, not only the <a> tags but also the <img> and any others, where links to other files are used.

Links to local addresses should be written without specifying a protocol and domain - just the path from the site's root - /job/vacancy.php . Links to other sites should be written completely - http://www.site1.ru/job/vacancy.php .


PHP provides many tools for working with files, directories and URLs.

First, there are numerous predefined variables that are described in the documentation and whose values ​​in your script can be looked up using phpinfo() :

The __FILE__ constant contains the name of the current executable.
Unlike PHP_SELF, it contains the name of the file being used at the moment.
Very useful is the design of dirname ( __FILE__ ) __FILE__ dirname ( __FILE__ ) dirname ( __FILE__ ) , On which it is desirable to replace all calls to files that are in the same directory as the calling script. For example:
require dirname ( __FILE__ ). "/init.php" Function dirname() , along with basename() is one of the most common for working with files and directories.

By phpfaq.ru