Retrieving Directory Name from File Path Name in PHP


How To Get the Directory Name out of a File Path Name in PHP?



If you have the full path name of a file, and want to get the directory name portion of the path name, you can use the dirname() function. It breaks the full path name at the last directory path delimiter (/) or (\), and returns the first portion as the directory name. Here is a PHP script example on how to use dirname():

$pathName = "/temp/download/todo.txt";
$dirName = dirname($pathName);
print("File full path name: $pathName\n");
print("File directory name: $dirName\n");

This script will print:

File full path name: /temp/download/todo.txt
File directory name: /temp/download


Working with Directoris and Files in PHP

⇒⇒PHP Tutorials

2016-11-20, 242👍, 0💬