For Example let the path be, $path = "protected/musics/composer/album/song.mp3"; I can extract the file name with the use of basename() function. $path = basename($path, ".mp3"); Also I want to extract the last two directory names in a variable example $composer_name = composer //(extracted from the path $path); $album_name = album //(extracted from the path $path) Could you please someone help me out of this? Thanks in advance.
<?php $path = "protected/musics/composer/album/song.mp3"; //a more effective way to get the actual filename, although not using it, I included it for your future needs. $filename = pathinfo($path, PATHINFO_BASENAME); $directory_parts = explode("/", dirname($path)); $composer_name = $directory_parts[2]; $album_name = end($directory_parts); ?> PHP: