Forum Moderators: coopster

Message Too Old, No Replies

how to extract the parent directory name from a path

         

siva1117

5:52 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



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.

Readie

6:16 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are all the paths in exactley this format?

$path = "protected/musics/composer/album/song.mp3";

If so:

$path = "protected/musics/composer/album/song.mp3";
$path = explode("/", $path);
$song = $path[4];
$album = $path[3];
$composer = $path[2];

siva1117

6:29 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



thank u so much Readie it works. It may be a silly question but I'm a newbie to PHP.