In our example it's /var/So now you can tell that to make any file system path work, it should be absolute and built using DOCUMENT_ROOT. To get the working path to this file, we have to add the missing part. And if you try to use it in PHP it will result in a failure: there is no /forum/ catalog on the HDD! Whereas for the script it's only a part of the full path - the filesystem path. Imagine there is a file like /var/While on the web-server its address is Īnd here the point can be clearly seen: there is a part, common for both addresses: /forum/index.php, which is the very source of confusion.įor the browser, this path is perfectly absolute, starting from the root of the web-server. There is a point where the real world meets the virtual one. Both web-server and file system are doing that but different ways. What you ought to know is that the system, when encountered a relative path, always builds it up to the absolute one. /file.php (file is in the folder that is two levels higher than the current directory) file.php (file is in the folder that is one level higher than the current directory) images/picture.jpg (the file is in the images folder that is in the current directory).file.php (the file is in the current folder. If your current directory is /about/ then index.html would be one, but if you switch it to /contacts/ then it will be another. So one should be careful with relative paths. The simplest example of relative path is just a file name, like index.html. If you don't supply the root, it means that your path is relative. on Windows it's either \ (for the current disk) or D:\ (system-wide).So now you can tell an absolute path from a relative one - it is starting from the root, which is: So you can tell that windows is rather confusing, but for the simplicity we would pretend that we have only one disk, and within its boundaries the rules are pretty much the same as in Unix. So you can type cd \ and get to the root of the current disk. Whereas each disk has its own root, which is a backslash - \. On Windows, the filesystem doesn't have the common root for the whole system but split between disks, so an absolute paths starts from the drive letter. So you can tell that in the address the trailing slash is not for the decoration but a regular address itself - the address of the home page. Exactly the same is true for all web servers. Type cd / in your unix console and you will get to the root directory. It is not just a marker, but already a full qualified address, a path. Note that in Unix-like systems (and web-servers) the root is defined as a slash - /. Some absolute path examples: /var/ www / site / forum / index. So again: an absolute path is one starting from the system root Whereas relative path is tricky, and should be used with caution, only when you positively know where you are at the moment. So it goes for the paths in the computer world: given the absolute address, you can always get to the place, no matter from where you started. However, given the relative directions, like "keep three blocks this way and then and turn to the right" would work from the current location only, otherwise sending you astray. Given the absolute address, a postal one, like "7119 W Sunset Blvd West Hollywood, CA 90046" you can find the location from anywhere. It's exactly the same as with the real life directions. If the path is built starting from the current location, it is called relative (which makes sense, as it is relative to our present position).If the path is built starting from the system root, it is called absolute.The difference between absolute and relative paths The difference between the root of the web server and the filesystem root.The difference between absolute and relative paths.However, to sort these things out all you need is to grasp just two simple concepts: PHP users confuse these matters badly at first, doing things like being unable to locate an existing file, confusing hyperlinks with files, including local files via HTTP and such. So this dualism is the root of many problems. And your PHP script, while reading data files or including other scripts, is working with such real files that exist on the physical medium. All it needs to know is an address.įor the site developer, on the other hand, their site is a certain program running on a particular server, on the very real computer with HDD, files and directories. Your browser cannot know that, and don't need to. There could be or could be not a real file with such a name, but it doesn't matter. In the address like, file.html is not a file. I know, it's hard to believe at first, but it's a fact. Your site exists in two realms at once: the real and the virtual one.įor the site visitors it's entirely a virtual server, which in many ways is different from a real one.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |