
$info = new SplFileInfo('/var/www/字形.woff') This means that for SplFileInfo::getBasename() to work properly with multibyte characters, a matching locale needs to be set first using the setlocale() function. Making SplFileInfo Methods Work With Multibyte Characters:Īccording to the docs, SplFileInfo::getBasename() is locale-aware while SplFileInfo::getFilename() is not. $info = new SplFileInfo('/path/to/directory/') Įcho $info->getBasename() // 'directory'Įcho $info->getFilename() // 'directory'įor the following cases, notice how with getFilename() method there's a leading slash while with getBasename() there is none: $info = new SplFileInfo('/var/www/DockerFile') Įcho $info->getBasename() // 'DockerFile'Įcho $info->getFilename() // 'DockerFile' $info = new SplFileInfo('/usr/john.doe/') Įcho $info->getBasename() // ''Įcho $info->getFilename() // '' $info = new SplFileInfo('/var/httpd/.htaccess') Įcho $info->getBasename() // '.htaccess'Įcho $info->getFilename() // '.htaccess' $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' 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.
