- Get Absolute Path in PHP: Explained with Examples
- What is an Absolute Path?
- How to Get the Absolute Path in PHP
- Method 1: Using the FILE constant
- Method 2: Using the realpath() function
- Method 3: Using the $_SERVER[‘DOCUMENT_ROOT’] variable
- Method 4: Using the getcwd() function
- Conclusion
- $_SERVER
- Indices
- Examples
Get Absolute Path in PHP: Explained with Examples
When working with PHP, it’s often necessary to get the absolute path of a file or directory on the server. This is because some functions and scripts require the absolute path to work properly. In this article, we’ll explain what absolute paths are, why they are important, and show you several examples of how to get the absolute path in PHP.
What is an Absolute Path?
An absolute path is a complete path that starts from the root of the file system and includes all the directories and subdirectories needed to get to a specific file or directory. For example, on a Unix-based system, the absolute path to the root directory is “/”.
Absolute paths are important because they provide an unambiguous way to locate a file or directory on the server, regardless of where the script is located or how the script was called.
How to Get the Absolute Path in PHP
There are several ways to get the absolute path in PHP. We’ll cover some of the most common methods.
Method 1: Using the FILE constant
The FILE constant returns the full path and filename of the current file. To get the absolute path of a file, you can use the dirname() function to extract the directory name from the FILE constant. Here’s an example:
This will give you the absolute path of the file that the code is being executed in.
Method 2: Using the realpath() function
The realpath() function returns the absolute path of a file or directory by resolving any symbolic links or references to parent directories. Here’s an example:
This will give you the absolute path of the file or directory specified by the relative path.
Method 3: Using the $_SERVER[‘DOCUMENT_ROOT’] variable
The $_SERVER[‘DOCUMENT_ROOT’] variable returns the root directory of the server, which is usually the directory where the website is installed. You can use this variable to construct the absolute path of a file or directory. Here’s an example:
This will give you the absolute path of the file or directory relative to the document root.
Method 4: Using the getcwd() function
The getcwd() function returns the current working directory of the script. You can use this function to construct the absolute path of a file or directory relative to the current working directory. Here’s an example:
This will give you the absolute path of the file or directory relative to the current working directory.
Conclusion
In this article, we explained what absolute paths are and why they are important. We also showed you several examples of how to get the absolute path in PHP using the FILE constant, the realpath() function, the $_SERVER[‘DOCUMENT_ROOT’] variable, and the getcwd() function. By using these methods, you can ensure that your PHP scripts work properly, regardless of where they are located on the server.
$_SERVER
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server, therefore there is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. However, most of these variables are accounted for in the » CGI/1.1 specification, and are likely to be defined.
Note: When running PHP on the command line most of these entries will not be available or have any meaning.
In addition to the elements listed below, PHP will create additional elements with values from request headers. These entries will be named HTTP_ followed by the header name, capitalized and with underscores instead of hyphens. For example, the Accept-Language header would be available as $_SERVER[‘HTTP_ACCEPT_LANGUAGE’] .
Indices
‘ PHP_SELF ‘ The filename of the currently executing script, relative to the document root. For instance, $_SERVER[‘PHP_SELF’] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php . The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name. ‘argv’ Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. ‘argc’ Contains the number of command line parameters passed to the script (if run on the command line). ‘ GATEWAY_INTERFACE ‘ What revision of the CGI specification the server is using; e.g. ‘CGI/1.1’ . ‘ SERVER_ADDR ‘ The IP address of the server under which the current script is executing. ‘ SERVER_NAME ‘ The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.
Note: Under Apache 2, UseCanonicalName = On and ServerName must be set. Otherwise, this value reflects the hostname supplied by the client, which can be spoofed. It is not safe to rely on this value in security-dependent contexts.
‘ SERVER_SOFTWARE ‘ Server identification string, given in the headers when responding to requests. ‘ SERVER_PROTOCOL ‘ Name and revision of the information protocol via which the page was requested; e.g. ‘HTTP/1.0’ ; ‘ REQUEST_METHOD ‘ Which request method was used to access the page; e.g. ‘GET’ , ‘HEAD’ , ‘POST’ , ‘PUT’ .
Note:
PHP script is terminated after sending headers (it means after producing any output without output buffering) if the request method was HEAD .
‘ REQUEST_TIME ‘ The timestamp of the start of the request. ‘ REQUEST_TIME_FLOAT ‘ The timestamp of the start of the request, with microsecond precision. ‘ QUERY_STRING ‘ The query string, if any, via which the page was accessed. ‘ DOCUMENT_ROOT ‘ The document root directory under which the current script is executing, as defined in the server’s configuration file. ‘ HTTPS ‘ Set to a non-empty value if the script was queried through the HTTPS protocol. ‘ REMOTE_ADDR ‘ The IP address from which the user is viewing the current page. ‘ REMOTE_HOST ‘ The Host name from which the user is viewing the current page. The reverse dns lookup is based on the REMOTE_ADDR of the user.
Note: The web server must be configured to create this variable. For example in Apache HostnameLookups On must be set inside httpd.conf for it to exist. See also gethostbyaddr() .
‘ REMOTE_PORT ‘ The port being used on the user’s machine to communicate with the web server. ‘ REMOTE_USER ‘ The authenticated user. ‘ REDIRECT_REMOTE_USER ‘ The authenticated user if the request is internally redirected. ‘ SCRIPT_FILENAME ‘
The absolute pathname of the currently executing script.
Note:
If a script is executed with the CLI, as a relative path, such as file.php or ../file.php , $_SERVER[‘SCRIPT_FILENAME’] will contain the relative path specified by the user.
‘ SERVER_ADMIN ‘ The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host. ‘ SERVER_PORT ‘ The port on the server machine being used by the web server for communication. For default setups, this will be ’80’ ; using SSL, for instance, will change this to whatever your defined secure HTTP port is.
Note: Under Apache 2, UseCanonicalName = On , as well as UseCanonicalPhysicalPort = On must be set in order to get the physical (real) port, otherwise, this value can be spoofed, and it may or may not return the physical port value. It is not safe to rely on this value in security-dependent contexts.
‘ SERVER_SIGNATURE ‘ String containing the server version and virtual host name which are added to server-generated pages, if enabled. ‘ PATH_TRANSLATED ‘ Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping.
Note: Apache 2 users may use AcceptPathInfo = On inside httpd.conf to define PATH_INFO .
‘ SCRIPT_NAME ‘ Contains the current script’s path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. ‘ REQUEST_URI ‘ The URI which was given in order to access this page; for instance, ‘ /index.html ‘. ‘ PHP_AUTH_DIGEST ‘ When doing Digest HTTP authentication this variable is set to the ‘Authorization’ header sent by the client (which you should then use to make the appropriate validation). ‘ PHP_AUTH_USER ‘ When doing HTTP authentication this variable is set to the username provided by the user. ‘ PHP_AUTH_PW ‘ When doing HTTP authentication this variable is set to the password provided by the user. ‘ AUTH_TYPE ‘ When doing HTTP authentication this variable is set to the authentication type. ‘ PATH_INFO ‘ Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URI http://www.example.com/php/path_info.php/some/stuff?foo=bar , then $_SERVER[‘PATH_INFO’] would contain /some/stuff . ‘ ORIG_PATH_INFO ‘ Original version of ‘ PATH_INFO ‘ before processed by PHP.
Examples
Example #1 $_SERVER example