Search code examples
phphtmlechohtml5-audioreaddir

PHP - Creating 'DYNAMIC' Directories For HTML5 <AUDIO>


So... I'm a mega noob when it comes to PHP, trying my best to learn but ITS NOT GOOD ENOUGH.

Anyways, what I am attempting to do is set up 2 different 'dynamic' directories within a PHP echo structured to fit an HTML5 audio tag.

- 1st directory for *.mp3
- 2nd directory for *.ogg

I'm trying to set it up so that I can just dump the corresponding file formats into their respective folders and voila! Auto generated HTML5 audio playback...

Should be easy enough, no? 100% sure I'm doing this the worst way possible.
Here's the code....

    <?PHP
        $handleAudioMp3 = opendir(dirname(realpath(__FILE__)).'/audio/mp3/');
        $handleAudioOgg = opendir(dirname(realpath(__FILE__)).'/audio/ogg/');
        while($fileMp3 = readdir($handleAudioMp3) & $fileOgg = readdir($handleAudioOgg)){
            if($fileMp3 !== '.' && $fileMp3 !== '..' && $fileOgg !== '.' && $fileOgg !== '..'){
                echo '<div>
                        <audio controls="controls" preload="none">
                            <source src="audio/mp3/'.$fileMp3.'"/>
                            <source src="audio/ogg/'.$fileOgg.'"/>
                        </audio>
                    </div>'     ;}}
                    closedir($handleAudioMp3);
                    closedir($handleAudioOgg);
    ?>

Revised & working thanks to ThiefMaster

<?PHP
        $handleAudioMp3 = opendir(dirname(realpath(__FILE__)).'/audio/mp3/');
        $handleAudioOgg = opendir(dirname(realpath(__FILE__)).'/audio/ogg/');
        while(($fileMp3 = readdir($handleAudioMp3)) & ($fileOgg = readdir($handleAudioOgg))){
            if($fileMp3 !== '.' && $fileMp3 !== '..' && $fileOgg !== '.' && $fileOgg !== '..'){
                echo '<div>
                        <audio controls="controls" preload="none">
                            <source src="audio/mp3/'.$fileMp3.'" type="audio/mpeg"/>
                            <source src="audio/ogg/'.$fileOgg.'" type="audio/ogg"/>
                        </audio>
                    </div>'     ;}}
                    closedir($handleAudioMp3);
                    closedir($handleAudioOgg);
    ?>


the difference is...

while(($fileMp3 = readdir($handleAudioMp3)) & ($fileOgg = readdir($handleAudioOgg))){

Solution

  • ThiefMaster pointed out that I had forgotten some parentheses within...

    while($fileMp3 = readdir($handleAudioMp3) & $fileOgg = readdir($handleAudioOgg)){
    

    So it should be...

    while(($fileMp3 = readdir($handleAudioMp3)) & ($fileOgg = readdir($handleAudioOgg))){
    

    Thanks Again Everyone!!!