How to give absolute path in php
In HTML, I can find a file starting from the web server's root folder by beginning the filepath with "/". Like: Show
I can put that path in any file in any subdirectory, and it will point to the right image. With PHP, I tried something similar:
...but that doesn't work. I think that that this page is saying that I can
set Using a . in the include path allows for relative includes as it means the current directory. Relative includes are exactly what I don't want. How do I make sure that all my includes point to the ClarificationMy development files are currently being served by XAMPP/Apache. Does that affect the absolute path? (I'm not sure yet what the production server will be.) UpdateI don't know what my problem was here. The One thing that occurs to me is that some people may have thought that "/some/path" was an "absolute path" because they assumed the OS was Linux. This server is Windows, so an absolute path would have to start with the drive name. Anyway, problem solved! :)
IntroYour site exists in two realms at once: the real and the virtual one. For the site visitors it's entirely a virtual server, which in many ways is different from a real one. There are no files for starter. I know, it's hard to believe at first, but it's a fact. In the address like For 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. And your PHP script, while reading data files or including other scripts, is working with such real files that exist on the physical medium. So this dualism is the root of many problems. 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. However, to sort these things out all you need is to grasp just two simple concepts:
The difference between absolute and relative pathsIt's fairly simple.
It's exactly the same as with the real life directions. Given the absolute address, a postal one, like "7119 W Sunset Blvd West Hollywood, CA 90046" you can find the location from anywhere. 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. 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. Whereas relative path is tricky, and should be used with caution, only when you positively know where you are at the moment. Absolute pathsSo again: an absolute path is one starting from the system root Some absolute path examples:
Note that in Unix-like systems (and web-servers) the root is defined as a slash - 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. Whereas each disk has its own root, which is a backslash - 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 now you can tell an absolute path from a relative one - it is starting from the root, which is:
Relative pathsIf you don't supply the root, it means that your path is relative. The simplest example of relative path is just a file name, like Other relative path examples:
What you ought to know is that the system, when encountered a relative path, always builds it up to the absolute one. Both web-server and file system are doing that but different ways. So, let's learn them. Document rootThis is the most interesting part. There is a point where the real world meets the virtual one. Imagine there is a file like And here the point can be clearly seen: there is a part, common for both addresses: For the browser, this path is perfectly absolute, starting from the root of the
web-server. To get the working path to this file, we have to add the missing part. In our example it's So now you can tell that to make
any file system path work, it should be absolute and built using
here we are using web-server part of the path, prepending it with the document root. Voila! Web server pathsare much simpler. Like it was said before, for the browser, there are no files on the server. A site user never has an access to the server's file system. For the browser, there is a site root only. Which is constant and always simply a slash. Therefore, to make an HTML link absolute, just build it from the site root - and you will never see a 404 error for the existing file again! Imagine your site has two sections,
and in the So it goes for all the internal links on the site - images, js and css files, hyperlinks or any other resource that can be clicked on or loaded on the page. For the local resources it's better to make it path only, without protocol and domain - like Console scripts. Single entry pointIt's a pity, but for the console scripts our useful For example, if your application is hosted in
Although technically absolute (starting from a slash), this path is essentially relative to the calling script, because if the calling script will be moved into another directory, it won't find the configuration file anymore. This is why it is recommended to use a single entry point for your application. Or - as in our case - two entry points, one for web requests and one for console commands. So for our fictional application we would have three files - an entry point for the web front, an entry point for console applications and a bootstrap file:
Then we could write the following code (among other things) in
to define the And then in both
to make all the bootstrap stuff available, including the ROOT_DIR constant. From now on we can use it to build absolute paths starting from the root directory (as long as our scripts are called through the entry point either web or console one):
Example implementations can be found in Laravel's Artisan or Symfony Console. Of course, both entry points should implement a sort of resolver to call all other pages and console scripts but that's slightly out of scope of this article. Helpful PHP commands and constantsThere are may hepful commands and constants in PHP to ease the path interpolation. Some of them are:
Related articles:
How do you create an absolute path?To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.
How do I set the path of a file in php?This article describes several methods for setting the include path in PHP.. Open the . htaccess file in an editor. ... . Add the following line to the .htaccess file. Replace path with the include directory's path: php_value include_path ".:/path". Save the file. The include path is now set.. How can I get full image path in php?If you mean the path where the image was located on the user computer before he/she uploaded it to your form - you can never know it in php or javascript or anywhere else. In PHP you can see the path on SERVER (usually in the temporary folder) where the file was stored so you can read or copy it.
What is __ DIR __ in php?The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
|