Duerdum9 Posted September 4, 2012 Share Posted September 4, 2012 Hello, I was wondering if you could help me with the following file, and help me how to fix my error. <?php $directory='./music/'; $extension=''; $files_array = array(); $dir_handle = @opendir($directory) or die("There is an error with your file directory!"); while ($file = readdir($dir_handle)) { if($file{0}=='.') {continue;} if($extension == 'php'){ continue;} if($extension == 'htaccess') {continue;} $files_array[]=$file; } $file_downloads=sort($files_array,SORT_STRING); $path_parts = pathinfo('musicogg'); foreach ($files_array as $file_name){ //echo "{mp3:\"music/$file_name.mp3\"}"; echo $path_parts['extension'], "\n"; } ?> (removed spaces to compress it). Anyhow, the last few lines of code, where I've commented a line of code out - that's working as it should by going into the dir and checking all the files and echo'ing them. So this is what I want. I want it the final code to do this execute this line of code. {mp3:"music/name.mp3",ogg:"music/name.ogg"} - Notice, I do not really care if they the files need to be in one dir, or two dirs. How would I do, so it would show that in HTML, of course with all the files in the dir(s). (Note, name.mp3 & name.ogg is the same song). Thanks a ton Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2012 Share Posted September 4, 2012 I'm not understanding everything you said. Especially about Notice, I do not really care if they the files need to be in one dir, or two dirs. It looks like the fields would always be in one directory. Anyway, I expect your problem is that the curly braces are in the wrong positions. Also, why hardcode the directory in that line when you made the directory a variable above? echo "mp3: \"{$directory}{$file_name}\"<br>\n"; Quote Link to comment Share on other sites More sharing options...
requinix Posted September 4, 2012 Share Posted September 4, 2012 JSON? The keys need to be quoted, and you can't return multiple objects at once. Taking a guess at what you want, the output should look like [{"mp3": "music/name.mp3", "ogg": "music/name.ogg"}, {"mp3": "music/name.mp3", "ogg": "music/name.ogg" }...] which you can do easily with json_encode(). Except your code needs changing to get that. What if there are multiple .mp3 or .ogg files? Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 The problem is that there's about 200-300 songs, and for being able to use my HTML5 player to work in (almost) all browsers, it needs both .mp3 and .ogg. Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 I'm not understanding everything you said. Especially about Notice, I do not really care if they the files need to be in one dir, or two dirs. It looks like the fields would always be in one directory. Anyway, I expect your problem is that the curly braces are in the wrong positions. Also, why hardcode the directory in that line when you made the directory a variable above? echo "mp3: \"{$directory}{$file_name}\"<br>\n"; http://themidlifecrisis.eu/musicplayer/ - Look at the source code. In the JS header - that's how I want the final output to be like, and of course way more songs. How would I do so? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2012 Share Posted September 5, 2012 Do you have an mp3 AND an ogg version of EVERY file? if so, then just do a search for mp3 (or ogg) files and build the output from that. If not, then you will need to process the jpb and ogg files into an array or some structure to verify those that are paired up and those that aren't. Then either create output only for those that are paired or allow them to be in the output as an unmatched pair. A couple other things. your logic int he while loop is overly complicated. Just check for the file types you are interested in instead of trying to program around all the other types. Also, the sort() function only returns true/false. It will sort the original array. So, the following lines that try to foreach() over the result will fail (unless I am reading something wrong) This might get you closer //Define directory to scan $directory='./music/'; //Define acceptable file types $extensions = array('jpg', 'ogg'); $filesArray = array(); $dir_handle = @opendir($directory) or die("There is an error with your file directory!"); while ($file = readdir($dir_handle)) { $filePath = $directory.$file; $info = pathinfo($filePath); if(in_array(strtolower($info['extension'])), $extensions) { $filesArray[$info['filename']][$info['extension']] = $filePath; } } sort($files_array, SORT_STRING); $path_parts = pathinfo('musicogg'); foreach ($filesArray as $fileName => $files) { if(count($files)==2) { echo "{mp3:\"{$files['mp3']}\",ogg:\"{$files['ogg']}\"}\n"; } } Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 What you should do is to use glob () to get all of the files & folders, within a recursive function to follow the sub-folders. Within it you'll need to construct an array with the following pattern: $Songs[] = array ('mp3' => $pathMP3, 'ogg' => $pathOGG); Once the function has run through all of the folders, you'll have a 2-dimensional array containing the information you need. Next step is to simply JSON encode it, and send the results to the client. Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 This code is broken, or something somehow: "if(in_array(strtolower($info['extension'])), $extensions)" - while getting this error "Parse error: syntax error, unexpected ','" - Tried removing the comma and stuff - doesn't work, any tips? Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 Oh yeah, and one thing - Everytime I upload the .mp3 files, I upload the .ogg files, so that wouldn't be a problem. So there's exactly the same amount of songs with both .mp3 and .ogg - and they're the same songs. Quote Link to comment Share on other sites More sharing options...
salathe Posted September 5, 2012 Share Posted September 5, 2012 getting this error "Parse error: syntax error, unexpected ','" ... any tips? Spot the difference: if(in_array(strtolower($info['extension'])), $extensions) - original if(in_array(strtolower($info['extension']), $extensions)) - fixed As for getting the right array or JSON, the replies above should help you out. Also, are all of the files in one folder? The guys above aren't sure whether you need to get files from one directory, several directories, or any level of directories. Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 It doesn't matter if they are in the same directory or in separate, but I prefer having them in separate directories. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 It may not matter to you, but when writing code it matters a whole lot. Since you prefer having them in separate directories, within one master directory (I presume/hope), then you'll need to use the recursive function as I described above. Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 It may not matter to you, but when writing code it matters a whole lot. Since you prefer having them in separate directories, within one master directory (I presume/hope), then you'll need to use the recursive function as I described above. So, two directories is simpler? As more simple is better & also, I don't know anything about JSON :L Quote Link to comment Share on other sites More sharing options...
salathe Posted September 5, 2012 Share Posted September 5, 2012 So, two directories is simpler? As more simple is better... No, not simpler. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2012 Share Posted September 5, 2012 So, two directories is simpler? As more simple is better... No, not simpler. YOU need to decide what makes sense for the problem at hand. If you want to put all the files in one directory it definitely makes the code simpler. But, if you don't that's fine too. You would just need to modify the code accordingly. A recursive function would make sense if all the files are in directories under a parent directory. But, if there are many other directories under that same parent that are not for music files that probably won't make sense. In that case, I'd define the "music" directories within an array and then process each of those in the code. But, on further consideration, I would ask how often you expect the files to change? It seems a waste to regenerate the javascript data for hundreds of files every time you load the web page. It might make sense to instead create a script to process the files and write a file with the data needed by the JavaScript. Then, include that code into the HTML page when you load it. If your music files change then rerun the processing script. Quote Link to comment Share on other sites More sharing options...
Duerdum9 Posted September 5, 2012 Author Share Posted September 5, 2012 It changes quite often. But I've got it working now, for all of you out there that're having a similar issue this is the source code: http://slypaste.org/kWSJ8Ygi Feel free to copy and edit it as your liking 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.