1

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))){
Terry
  • 675
  • 2
  • 9
  • 21
  • 2
    Try adding some parentheses. `&` is not meant to be used in this way so it doesn't have the same priorities as `&&`. That might not fix your code though since `readdir()` results are not sorted – ThiefMaster Jan 14 '12 at 12:35
  • ThiefMaster, Thanks for the quick reply! Not sure if I exactly understand... do you mean within... while($fileMp3 = readdir($handleAudioMp3) & $fileOgg = readdir($handleAudioOgg)){ changing & to &&??? I know I'm going about this the wrong way but I'm sure sure what to do. – Terry Jan 14 '12 at 12:52
  • 1
    `while((a = b) & (c = d))` instead of `while(a = b & c = d)` – ThiefMaster Jan 14 '12 at 13:01
  • THANK YOU, THANK YOU, THANK YOU, THANK YOU, THANK YOU, THANK YOU,THANK YOU, THANK YOU, THANK YOU, THANK YOU, THANK YOU... THANK YOU @ThiefMaster!!! flew right over my head! [answer] while((a = b) & (c = d)) instead of while(a = b & c = d) – Terry Jan 14 '12 at 13:47

2 Answers2

0

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!!!

Terry
  • 675
  • 2
  • 9
  • 21
0

There are many ways to skin a cat, here are two:

  1. Let the user decide (e.g. via a select) which format he wants, then use only this format
  2. display not 2 sources for 1 audio tag, but 2 audio tags, labelling them "play ogg" and "play mp3§
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • Thanks for the quick reply Eugen Rieck! Gonna do some think'n about it but from my understanding having 2 different tags would render 1 set not functional/dead in either Firefox or Webkit... No? – Terry Jan 14 '12 at 12:47
  • Letting the user choose is not really acceptable for ` – ThiefMaster Jan 14 '12 at 13:02
  • While this is the theory, my experience is quite different: Multiple source definitions have proved problematic for an app intended to run on "all" browsers on "all" platforms. – Eugen Rieck Jan 14 '12 at 13:15
  • My experiences differ... think it might have something to do with varying build versions? – Terry Jan 14 '12 at 13:21
  • I set up two tags... and... file.ogg works in firefox but not webkit... file.mp3 works in webkit but not firefox... Thus there are two players present but only one is functional... by specifying dirrent within the – Terry Jan 14 '12 at 13:29