How to get a file name from a full path with PHP?

Spread the love

Sometimes, we want to get a file name from a full path with PHP.

In this article, we’ll look at how to get a file name from a full path with PHP.

How to get a file name from a full path with PHP?

To get a file name from a full path with PHP, we use the SplFileInfo class.

For instance, we write

$info = new SplFileInfo("/path/to/foo.txt");
var_dump($info->getFilename());

to create a SplFileInfo object with the path string.

Then we get the file name string from the path string with

$info->getFilename()

Conclusion

To get a file name from a full path with PHP, we use the SplFileInfo class.

Leave a Reply

Your email address will not be published.