PHP: Get last directory name from path PHP: Get last directory name from path php php

PHP: Get last directory name from path


Easiest way would be to use basename($yourpath) as you can see here: http://php.net/basename


Provided answer doesn't work if your string contains the file at the end, like :

basename('/home/mypath/test.zip');

gives

test.zip

So if your string contains the file, don't forget to dirname it first

basename(dirname('/home/mypath/test.zip'));

gives

mypath


This is the easiest way:

<?php  echo basename(getcwd());?>

getcwd() = give your full directory pathbasename() = give you last directory