Jump to content

Recommended Posts

I'm not too fluent i php, so this may be ridiculously simple;

 

My php project is a sort of database of my collection of mp3 files. They are all stored on my hard drive/external hard drive (C: and E:). The problem is how to load these files from the php application (running on apache server) - the project is going to stay on my pc, not to be uploaded to a different server - I'm guessing the problems with the server, finding c: ?

I'll be using a javascript to play the files, or just embed that Quicktime thing.

 

This does not work:

 

$fil2 = "'file:///C:/RadioDB/2-4 Murder Unprompted.mp3'>" ;

 

or this:

<a href='http://C:\RadioDB/2-4 Murder Unprompted.mp3'>

 

Any help appreciated!

/jon

 

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/
Share on other sites

$genres = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge", "Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B", "Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska", "Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient", "Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical", "Instrumental","Acid","House","Game","Sound Clip","Gospel", "Noise","AlternRock","Bass","Soul","Punk","Space","Meditative", "Instrumental Pop","Instrumental Rock","Ethnic","Gothic", "Darkwave","Techno-Industrial","Electronic","Pop-Folk", "Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta", "Top 40","Christian Rap","Pop/Funk","Jungle","Native American", "Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer", "Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro", "Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock", "National Folk","Swing","Fast Fusion","Bebob","Latin","Revival", "Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock", "Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band", "Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson", "Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus", "Porn Groove","Satire","Slow Jam","Club","Tango","Samba", "Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle", "Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall");

$directories = array('path/to/directory1', 'path/to/directory2', ..);
foreach ($directories as $directory) {
    foreach (glob("$directory/*.mp3") as $file) {
        $fh = fopen($file, 'r');
        fseek($fh, -128, SEEK_END);
        
        $tag = fread($fh, 3);
        $data = array();
        if ('TAG' === $tag) {
            $data['song'] = fread($fh, 30);
            $data['artist'] = fread($fh, 30);
            $data['album'] = fread($fh, 30);
            $data['year'] = fread($fh, 4);
            $data['comment'] = fread($fh, 30);
            
            $genre = fread($fh, 1);
            $data['genre'] = $genres[ord($genre)];
            
            foreach ($data as $key => $value) {
                //echo $key, ' = ', $value, '<br>';
            }
        }
    }
}

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-902581
Share on other sites

Thanks ignace!

Your code get met a lot closer to what I want, but not quite there...

 

The $directory and $file works fine. The $fh gives me (when I echo it) a Resource id - is it this I'm going to open/play the file with?

 

 

BTW, I've tried to implement the javascript for playing mp3 from this site:

http://www.oreillynet.com/pub/a/oreilly/digitalmedia/2006/05/31/build-a-better-web-audio-player.html?page=2  (the "BatmoAudioPop.js")

 

(This method works great - when I try it on a mp3 file stored in the same folder (easyphp/www/myfolder/) as my php application).

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903304
Share on other sites

Thanks ignace!

Your code get met a lot closer to what I want, but not quite there...

 

The $directory and $file works fine. The $fh gives me (when I echo it) a Resource id - is it this I'm going to open/play the file with?

 

 

BTW, I've tried to implement the javascript for playing mp3 from this site:

http://www.oreillynet.com/pub/a/oreilly/digitalmedia/2006/05/31/build-a-better-web-audio-player.html?page=2  (the "BatmoAudioPop.js")

 

(This method works great - when I try it on a mp3 file stored in the same folder (easyphp/www/myfolder/) as my php application).

 

Yeah you actually need recursion to go through the entire directory structure capture each and every mp3 file and read it's ID3

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903371
Share on other sites

function scandir_r($directory) {
    $mp3files = array();
    if (!is_dir($directory) || !is_readable($directory)) return $mp3files;
    $files = scandir($directory);
    foreach ($files as $file) {
        $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
        if (is_dir($fullpath)) {
            $mp3files += call_user_func(__FUNCTION__, $fullpath);
        } else if (preg_match('/\.mp3/', $file)) {
            $mp3files[] = $fullpath;
        }
    }
    return $mp3files;
}

$mp3files = array();
$directories = array('..');
foreach ($directories as $directory) {
    $mp3files += scandir_r($directory);
}

print_r($mp3files);

 

Edit: I haven't tested this. I just wrote this out of the top of my head. If you have a serious amount of mp3 files your script may time-out and you may want to look at other algorithms available.

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903378
Share on other sites

Thanks again, ignace!

You're right about the time-out, but my biggest problem is that I don't quite understand what your last suggestion will do - I'm not that good at php...

Will the $mp3files function return a filepath that I can use (with the javascript, for example) ?

 

Jon

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-904502
Share on other sites

Thanks again, ignace!

You're right about the time-out, but my biggest problem is that I don't quite understand what your last suggestion will do - I'm not that good at php...

Will the $mp3files function return a filepath that I can use (with the javascript, for example) ?

 

Jon

 

Yes it will return the full path to the file but you won't be able to use this with JavaScript.

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-904923
Share on other sites

Here's the code that I was playing with

 

function getPlayLength($mp3file) {
    $filesize = filesize($mp3file);
    return 0;
}

function getMusicGenre($genreId, $default = null) {
    static $genres = array(
        'Blues', 'Classic Rock', 'Country', 'Dance', 'Disco',
        'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal', 'New Age',
        'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock',
        'Techno', 'Industrial', 'Alternative', 'Ska', 'Death Metal',
        'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient', 'Trip-Hop',
        'Vocal', 'Jazz+Funk', 'Fusion', 'Trance', 'Classical',
        'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip', 'Gospel',
        'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk', 'Space', 'Meditative',
        'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 'Gothic',
        'Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk',
        'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta',
        'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American',
        'Cabaret', 'New Wave', 'Psychadelic', 'Rave', 'Showtunes', 'Trailer',
        'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro',
        'Musical', 'Rock & Roll', 'Hard Rock', 'Folk', 'Folk-Rock',
        'National Folk', 'Swing', 'Fast Fusion', 'Bebob', 'Latin', 'Revival',
        'Celtic', 'Bluegrass', 'Avantgarde', 'Gothic Rock', 'Progressive Rock',
        'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band', 'Chorus',
        'Easy Listening', 'Acoustic', 'Humour', 'Speech', 'Chanson', 'Opera',
        'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass', 'Primus',
        'Porn Groove', 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba',
        'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle',
        'Duet', 'Punk Rock', 'Drum Solo', 'Acapella', 'Euro-House', 'Dance Hall'
    );
    return isset($genres[$genreId]) ? $genres[$genreId] : $default;
}

function getId3Tag($mp3file) {
    $data = array();
    if ($fh = fopen($mp3file, 'r')) {
        fseek($fh, -128, SEEK_END);
        
        $tag = fread($fh, 3);
        if ('TAG' === $tag) {
            $data['song'] = trim(fread($fh, 30));
            $data['artist'] = trim(fread($fh, 30));
            $data['album'] = trim(fread($fh, 30));
            $data['year'] = trim(fread($fh, 4));
            $data['comment'] = trim(fread($fh, 30));
            //$data['length'] = getPlayLength($mp3file);
            $data['genre'] = getMusicGenre(ord(fread($fh, 1)));
        }
    }
    return $data;
}

function getMusicFiles($directory) {
    $mp3files = array();
    if (!is_dir($directory) || !is_readable($directory)) return $mp3files;
    $files = scandir($directory);
    foreach ($files as $file) {
        if ('.' === $file[0] || !preg_match('/\.(mp3|mp4|org|wav|wma)$/', $file)) continue;
        $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
        if (is_dir($fullpath)) {
            $mp3files = array_merge($mp3files, call_user_func(__FUNCTION__, $fullpath));
        } else {
            $mp3files[] = $fullpath;
        }
    }
    return $mp3files;
}

Link to comment
https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-905225
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.