zenmind Posted October 4, 2012 Share Posted October 4, 2012 (edited) Hi guys I needed some help and I found this forum so I thought I'd give it a try. I have this lab that Im doing and instructor really didnt go into detail on how to do it even the code that was demoed was not really helpful in the class . My issue is that I cant seem to get my foreach loop to read the files in the directory i specified and display the name along with being able to play the file as well . Any pointers would be great its over my head at this point . Thanks <!DOCTYPE html > <head> <title>Music Viewer</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="http://www.cs.washington.edu/education/courses/cse190m/09sp/labs/3-music/viewer.css" type="text/css" rel="stylesheet" /> </head> <body> <div id="header"> <h1>190M Music Playlist Viewer</h1> <h2>Search Through Your Playlists and Music</h2> </div> <div id="listarea"> <ul id="musiclist"> <!-- *means is equivalent to windows explorer search --> <?php foreach (scandir(C:\Users\test\Desktop\Main\xampp\htdocs\mp3)("./mp3/*.mp3") as $song) { ?> <li class="mp3item"> <a href="./<?= $song ?>"> <?= basename($song) ?> </a> <!--end of href -- > </li> </ul> </div> </body> </html> Edited October 4, 2012 by zenmind Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 4, 2012 Share Posted October 4, 2012 (edited) You should be getting errors based upon that code - but you chose not to include them? When you are writing code, especially as a beginning, do one thing and verify it works before doing the next thing. In this case you should verify that you are getting the results of the files. So, I wouldn't even use a foreach() loop to produce output, just use a print_r() on the array. Also, don't put too much onto one line. Here is your problem: foreach (scandir(C:\Users\test\Desktop\Main\xampp\htdocs\mp3)("./mp3/*.mp3") as $song) So, don't do a foreach() on the function results. Instead put the function results into a variable and use that. But, look at the directory path you are using for scan directory. You are supposed to pass a string to the function. You should either set the path as a variable OR you need to enclose the string in quote marks: $directory = 'C:\Users\test\Desktop\Main\xampp\htdocs\mp3'; $files = scandir($directory); // print_r($files); //Uncomment to debug foreach($files as $file) { //Do something } Edited October 4, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
zenmind Posted October 5, 2012 Author Share Posted October 5, 2012 hmm ok its easier or better just to print the contents of the array itself .. I tried that code and messed around with it I ran in via localhost(xampp) and this what the page returned: Gigi D'Agostino - L'Amour Toujours ( Official Video ).mp3 [4] => KennedyKarate.mp3 ) Array ( [0] => . [1] => .. . So it its printing the array but I want just the song name to show with the mp3 file logo and also when clicked it plays it within the browser Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted October 5, 2012 Share Posted October 5, 2012 Your problem is that you end your foreach before you even start it. foreach (blah blah blah) {?> That is way wrong! What you want is something like this foreach (something happens) { do something here } You wanna include your list in the the do something here Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 5, 2012 Share Posted October 5, 2012 Closing the php tag set after the opening curly brace is perfectly valid, and doesn't end the foreach loop. You can exit and enter php pretty much at will as long as the code is syntactically correct. <?php $array = range( 1,9 ); foreach( $array as $v ) {?> Number: <?php echo $v ?><br> <?php } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 5, 2012 Share Posted October 5, 2012 Your problem is that you end your foreach before you even start it. foreach (blah blah blah) {?> That is way wrong! There is nothing wrong with that ^^^. He didn't end the foreach loop by closing the PHP tag. In fact, he actually never ended the loop. Although I abhore opening and closing PHP tags multiple times in my script there is nothing wrong with doing that. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 5, 2012 Share Posted October 5, 2012 hmm ok its easier or better just to print the contents of the array itself .. I tried that code and messed around with it I ran in via localhost(xampp) and this what the page returned: Gigi D'Agostino - L'Amour Toujours ( Official Video ).mp3 [4] => KennedyKarate.mp3 ) Array ( [0] => . [1] => .. . So it its printing the array but I want just the song name to show with the mp3 file logo and also when clicked it plays it within the browser OK, so what did I tell you before? Do one thing, make sure it works, then do the next thing. So, now you know you have the data in an array. Now loop over the array to echo the file name (which I assume to be the same as the song name) to the page. Then you'll need to create a link to each file so the user can play them. You'll need to make these links relative to the file from the root of the web folder, not the C drive. Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 5, 2012 Share Posted October 5, 2012 (edited) As for the directory being specified, you have done it slightly wrong - you are using xampp, this means that htdocs (to a browser) doesnt technically exist and is replaced with "http://localhost/siteName" to prove this, echo this out: $filename = $_SERVER["DOCUMENT_ROOT"]; echo $filename; This means this line: $directory = 'C:\Users\test\Desktop\Main\xampp\htdocs\mp3'; Should be: $directory = $_SERVER["DOCUMENT_ROOT"]."/mp3/"; Edited October 5, 2012 by White_Lily Quote Link to comment Share on other sites More sharing options...
zenmind Posted October 7, 2012 Author Share Posted October 7, 2012 (edited) Ok I think im getting somewhere thanks to everyone so far helping me through this. I decided not to have a loop because I dont have many items to display and I think its a waste of time to use a foreach in this small scenario since I only need links to play a file when clicked this is what I have modified so far: <?php $directory = 'C:\Users\test\Desktop\Main\xampp\htdocs\mp3'; $files = scandir($directory); // print_r($files); //Uncomment to debug /*foreach($files as $file) { /* for now dont put any code in the body of foreach loop to test songs */ //print the files in the array //print_r($files); //} ?> <li class="mp3item"> <a href= "C:\Users\test\Desktop\Main\xampp\htdocs\mp3">Gigi D'Agostino - L'Amour Toujours</a> <?= @basename($song) ?> </li> <li class="mp3item"> <a href= "C:\Users\test\Desktop\Main\xampp\htdocs\mp3">KennedyKarate</a> <?= @basename($song) ?> I'm trying to find a way which will allow me to play the file I don't know why clicking doesn't do anything even though the correct directory is given and everything is there something in php I'm missing ? Edited October 7, 2012 by zenmind Quote Link to comment Share on other sites More sharing options...
ignace Posted October 7, 2012 Share Posted October 7, 2012 (edited) It's better to use a loop. Your instructor may put in some other songs when reviewing your assignment. You are pointing to a directory instead of an actual file, the correct would be: <a href="C:\Users\test\Desktop\Main\xampp\htdocs\mp3\Gigi D'Agostino - L'Amour Toujours.mp3">Gigi D'Agostino - L'Amour Toujours</a> (Great music choice BTW ^^) When you now click, it will launch your default music program. Make the path relative or it won't work on your instructor's computer which likely results in 0, like this: <a href="mp3/Gigi D'Agostino - L'Amour Toujours.mp3">Gigi D'Agostino - L'Amour Toujours</a> This is relative to your htdocs. htdocs `- mp3/ `- index.php In your index.php you are now pointing to the mp3/ directory relatively since both are in the document root (htdocs). Like already mentioned you should turn on error_reporting your code is littered with syntax errors. error_reporting(-1); ini_set('display_errors', 1); -- Since the HTML5 effort it's possible to play music directly into a limited set of browsers: <?php $mp3files = new GlobIterator('mp3/*.mp3'); ?> <ul> <?php foreach ($mp3files as $mp3file): ?> <li class="mp3item"> <audio controls="controls"> <!-- 1. play the file if the browser supports it. --> <source src="mp3/<?= htmlentities($mp3file->getFilename(), ENT_QUOTES) ?>" type="audio/mpeg"> <!-- 2. embed flash player here and let flash play the file --> <!-- 3. let them download the file and play it in their default music player --> <a href="mp3/<?= urlencode($mp3file->getFilename()) ?>" class="mp3"> <?= htmlentities($mp3file->getFilename()) ?> </a> </audio> </li> <?php endforeach ?> </ul> What this does is list each file and: * present a play button if your browser supports it or * present a download link. Edited October 7, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
zenmind Posted October 7, 2012 Author Share Posted October 7, 2012 Wow! That worked thank you so much! i love Gigi by the way and all dance and trance type of music I know in Europe its way more popular than over here in the states. So As Im going through the code so I understand it seems this is html5 code <li class="mp3item"> <audio controls="controls"> <!-- 1. play the file if the browser supports it. --> <source src="mp3/<?= htmlentities($mp3file->getFilename(), ENT_QUOTES) ?>" type="audio/mpeg"> <!-- 2. embed flash player here and let flash play the file --> <!-- 3. let them download the file and play it in their default music player --> <a href="mp3/<?= urlencode($mp3file->getFilename()) ?>" class="mp3"> <?= htmlentities($mp3file->getFilename()) ?> </a> </audio> and you have some PHP right before it : <!-- Invoked GlobIterator which iterates through a filesystem to find matching file names --> <?php $mp3files = new GlobIterator('mp3/*.mp3'); ?> <ul> <!--Foreach loop --> correct? Also if I renable the orginal foreach to go through all the songs i have in the htdocs directory in xampp the original music list still appears but also the data in the foreach is shown like this: Gigi D'Agostino - L'Amour Toujours ( Official Video ).mp3 [4] => KennedyKarate.mp3 ) Array ( [0] => . [1] => .. I also wanted to know more about this : Is it actually code which makes the files relative to the person using them or about to stream them? htdocs `- mp3/ `- index.php and for the error reporting code can I just place that in between a new set of PHP tags or can it be placed at the end of any PHP segment? Thank you ignace for all your help so far! Quote Link to comment Share on other sites More sharing options...
ignace Posted October 7, 2012 Share Posted October 7, 2012 (edited) i love Gigi by the way and all dance and trance type of music I know in Europe its way more popular than over here in the states. Well, not anymore, but he was insanely popular between 2000 and 2003. A lot of great music genres have disappeared over the years like the and the or (you might wanna reduce your sound volume before clicking). -- Yes like I already mentioned it's html5 Also if I renable the orginal foreach to go through all the songs i have in the htdocs directory in xampp the original music list still appears but also thedata in the foreach is shown like this: That's because the original foreach contains a print_r() which is for debugging. Don't use the original foreach use mine as otherwise it won't be able to read the mp3 files out of the directory. I also wanted to know more about this :Is it actually code which makes the files relative to the person using them or about to stream them? htdocs `- mp3/ `- index.php No it is a textual representation of how I assumed your directory structure to be. and for the error reporting code can I just place that in between a new set of PHP tags or can it be placed at the end of any PHP segment? Just put it here: <?php error_reporting(-1); ini_set('display_errors', 1); $mp3files = new GlobIterator('mp3/*.mp3'); ?> Edited October 7, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
zenmind Posted October 7, 2012 Author Share Posted October 7, 2012 thanks for all the help yea I just did a whole bunch of reading in the past couple hours especially on the error reporting in the php.ini file you saved a lot of headaches for me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.