Jump to content

MP3 in Folder


Fijiannn

Recommended Posts

Hi I have this code below. What I'm trying to do below is access .mp3 songs that are in my folder called "songs" When I do this code it doesn't show anything for the Artist, Title or anything anymore. Why is that?

<?php
    require_once('/home/manveers94/public_html/getID3-1.9.7/getid3/getid3.php');
    $getID3 = new getID3;
    $SongsFolder = "songs";
    $file = glob($SongsFolder."/*.mp3");
   
    // $file = 'Song.mp3'
    
    set_time_limit(30);
    $ThisFileInfo = $getID3->analyze($file);
    getid3_lib::CopyTagsToComments($ThisFileInfo);
    
    echo 'File name: '.$ThisFileInfo['filenamepath'].'<br>';
    echo 'Artist: '.(!empty($ThisFileInfo['comments_html']['artist']) ? implode('<BR>', $ThisFileInfo['comments_html']['artist']) : ' ').'<br>';
    echo 'Title: '.(!empty($ThisFileInfo['comments_html']['title']) ? implode('<BR>', $ThisFileInfo['comments_html']['title'])  : ' ').'<br>';
    echo 'Bitrate: '.(!empty($ThisFileInfo['audio']['bitrate']) ? round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps'   : ' ').'<br>';
    echo 'Play time: '.(!empty($ThisFileInfo['playtime_string']) ? $ThisFileInfo['playtime_string']                          : ' ').'<br>';
?>
Link to comment
https://forums.phpfreaks.com/topic/285125-mp3-in-folder/
Share on other sites

$file is an array of filenames, not a single filename. You have to loop over it.

 

$file is an array of filenames, not a single filename. You have to loop over it.

 

I tried looping and getting these errors. 

 

Warning: pathinfo() expects parameter 1 to be string, array given in /home/manveers94/public_html/yee/Test.php on line 16

 

Notice: Undefined index: filenamepath in /home/manveers94/public_html/yee/Test.php on line 25

<?php
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    ini_set('output_buffering','Off');
    error_reporting(-1);
    
    require_once('/home/manveers94/public_html/getID3-1.9.7/getid3/getid3.php');
    $getID3 = new getID3;
   
    $SongsFolder = "songs";
    $file = glob($SongsFolder."/*.mp3");
    
    
    foreach($file as $SongPath)
    {
        $SongPath = pathinfo($file);
    }
    
    // $file = 'Song.mp3'
    
    set_time_limit(30);
    $ThisFileInfo = $getID3->analyze($SongPath);
    getid3_lib::CopyTagsToComments($ThisFileInfo);
    
    echo 'File name: '.$ThisFileInfo['filenamepath'].'<br>';
    echo 'Artist: '.(!empty($ThisFileInfo['comments_html']['artist']) ? implode('<BR>', $ThisFileInfo['comments_html']['artist']) : ' ').'<br>';
    echo 'Title: '.(!empty($ThisFileInfo['comments_html']['title']) ? implode('<BR>', $ThisFileInfo['comments_html']['title'])  : ' ').'<br>';
    echo 'Bitrate: '.(!empty($ThisFileInfo['audio']['bitrate']) ? round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps'   : ' ').'<br>';
    echo 'Play time: '.(!empty($ThisFileInfo['playtime_string']) ? $ThisFileInfo['playtime_string']                          : ' ').'<br>';
?>
Link to comment
https://forums.phpfreaks.com/topic/285125-mp3-in-folder/#findComment-1464018
Share on other sites

Hey,

 

In your foreach, your pathinfo is still looking at $file instead of $SongPath, meaning you're still looking at the array rather than a string.

 

Change your foreach to

 

foreach($file AS $filename) {
    $SongPath = pathinfo($filename);
}
Then it should work as expected.

 

Note that you don't need to change the whole foreach, you could just change pathinfo($file) to pathinfo($SongPath). I suggested changing the foreach just to name your variables a bit better.

 

Why are you looping through it just to get a path and not doing any other code inside the foreach? You know that you'll only ever get the last element in the array? Unless that's your intended behaviour, you should look at maybe using something like pathinfo($file[0]) instead of foreaching if you only ever want the first one.

Link to comment
https://forums.phpfreaks.com/topic/285125-mp3-in-folder/#findComment-1464037
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.