

$info = new SplFileInfo('/var/$info->getFilename() // 'index.php' While using either one, be cautious about the differences between the two as shown in some of the following examples: In the SplFileInfo class we have two methods that we can use to get the filename with extension: $pathChunks = explode(DIRECTORY_SEPARATOR, '/var/www/字形.woff') In case of explode() you could potentially substitute it with mb_split(). However, if possible, it is always a better option to use locale aware or multibyte string functions when working with multibyte strings. Multibyte characters should work fine with explode() as long as the string contains well-formed multibyte sequences. $pathChunks = explode(DIRECTORY_SEPARATOR, '/foo/') $pathChunks = explode(DIRECTORY_SEPARATOR, '/foo.txt') $pathChunks = explode(DIRECTORY_SEPARATOR, '/') $pathChunks = explode(DIRECTORY_SEPARATOR, '.') returning the trailing component when string does not end with a directory separator and empty otherwise. From the perspective of getting filename with an extension, this is presumably the correct behavior - i.e. Notice how this returns an empty string for a path ending with a directory separator. The key difference of using explode() (for the purpose of getting the filename with extension) is the example above. $pathChunks = explode(DIRECTORY_SEPARATOR, '/path/to/directory/') $pathChunks = explode(DIRECTORY_SEPARATOR, '/var/www/DockerFile') $pathChunks = explode(DIRECTORY_SEPARATOR, '/usr/john.doe/') Įcho end($pathChunks) // '' $pathChunks = explode(DIRECTORY_SEPARATOR, '/var/httpd/.htaccess') $pathChunks = explode(DIRECTORY_SEPARATOR, '/var/www/html/index.php') $pathInfo = pathinfo('/var/To see how this is done, let's consider the following examples: Since pathinfo() is locale-aware, it won't work with multibyte characters correctly till a matching locale is set first using the setlocale() function. Making pathinfo() Work With Multibyte Characters: but neither filename nor extension do the same. In the example above, notice how basename returns. $pathInfo = pathinfo('/path/to/directory/') Įcho $pathInfo // 'directory' $pathInfo = pathinfo('/var/www/DockerFile') Įcho $pathInfo // 'DockerFile' $pathInfo = pathinfo('/usr/john.doe/') Įcho $pathInfo // ''Įcho $pathInfo // 'my-file.tar'


$pathInfo = pathinfo('/var/httpd/.htaccess') Įcho $pathInfo // '.htaccess'Įcho $pathInfo // 'htaccess' $pathInfo = pathinfo('/var/We could have also used $pathInfo and $pathInfo with pathinfo() to get filename and extension individually. Or, the same can be written without passing an option as the second argument to pathinfo(), in which case it will return an associative array containing information about the file path. Following are two ways in which we can retrieve the filename and extension from a file path using pathinfo():Įcho pathinfo('/var/We could have also used PATHINFO_FILENAME and PATHINFO_EXTENSION constants with pathinfo() to get filename and extension separately. The pathinfo() function can be used to return information about a file path. Since basename() is locale-aware, it won't work with multibyte characters correctly till a matching locale is set first using the setlocale() function. Making basename() Work With Multibyte Characters: To demonstrate how this works, let's look at a few examples:Įcho basename('/var/httpd/.htaccess') // '.htaccess'Įcho basename('/usr/john.doe/') // ''Įcho basename('/var/www/DockerFile') // 'DockerFile'Įcho basename('/path/to/directory/') // 'directory' However, I do believe in this case, that all you need is the relative path.We can use basename() to return the filename and extension from a file path, since the function returns the trailing component from a string containing a path to a file or directory.
#PHP GET FILE PATH OF CURRENT FILE FULL#
If you would like to get the full url, you can do something like:Įcho $_SERVER. $_SERVER – gives the correct HTTP(S) protocol and domain name. $_SERVER – gives the route of the current file (after the domain name) There is also a function to parse URLs built in to PHP: The directory the script is located in on the servers filesystem, and the URL, are 2 completely different things. That gives you the location on the filesystem/server (not the URL) that your script is running from. Please note that echo getcwd() is not what you want, based on your question. In the case above that will give you /~andy (without test.php at the end). If you want the directory your current script is running in – without the filename – use: For example if the URL is The output would be:
