skot Posted May 24, 2008 Share Posted May 24, 2008 Hi Wondering if anyone can help me on this one.. I basically have a script that lists all files within a directory and sets the date created of the file to $adate. The format of a date is 'd/m/y', and i thought the php sort function would have trouble with that so i also set $date2 to be in the date format 'dmy'. I am trying to sort my array by date created, so am using the following but i suspect this isnt the correct syntax as it's not working. The files seem to be sorted in a random order now. Here's the code, any ideas? : <?php // open dir $myDirectory = opendir("."); // fetch entries while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close dir closedir($myDirectory); // count elements in array $indexCount = count($dirArray); $extras = 1; $goodcount = ($indexCount - $extras); Print ("<font face=verdana size=3 color=#F8F304><B>:: To download right click on a track and 'save as...'</B><br>:: To upload click the link at the top-right.<br><br>:: Current Track Count: $goodcount </font>"); // sort by date created sort($dirArray, $date2); // print results print("<center><TABLE width=95% border=1 align=left cellpadding=0 cellspacing=0 bordercolor=#183041>\n"); print("<TR align=left background=http://bridgey.net/img/row_bg.gif><TH><font face=verdana size=2><B>Name</TH><th>Type</th><th>File Size</th><th>Date Added</th></B></font></TR>\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print("<TR background=http://bridgey.net/img/row_bg.gif><TD align=left background=http://bridgey.net/img/row_bg.gif><font face=verdana size=2 color=#D9F4FD><b> <a href=\"$dirArray[$index]\"><img src=../img/star.gif border=0> $dirArray[$index]</a></b></font></td>"); print("<td background=http://bridgey.net/img/row_bg.gif>"); //$1file = ($dirArray[$index]); $ext = array_pop(explode(".", ($dirArray[$index]))); //print(filetype($dirArray[$index])); print($ext); print("</td>"); print("</td>"); print("<td background=http://bridgey.net/img/row_bg.gif>"); $sizeinmb2 = filesize($dirArray[$index]); $goodsize = ($sizeinmb2 / 1024 / 1024); $mb = round($goodsize, 2); print($mb." MB"); print("</td>"); print("<td background=http://bridgey.net/img/row_bg.gif>"); $adate = date("d/m/y", filemtime($dirArray[$index])); $date2 = date("dmy", filemtime($dirArray[$index])); print($adate); print("</td>"); print("</TR>\n"); } } print("</center></TABLE>\n"); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2008 Share Posted May 24, 2008 If you have the dates in the format dmy there are two problems. First you need to be using leading zeros for month and day as needed (i.e. ddmmyyyy). Also, that will not sort in chronological order. You need to use the format yyyymmdd. Quote Link to comment Share on other sites More sharing options...
skot Posted May 24, 2008 Author Share Posted May 24, 2008 I've now set $date2 = date("Ymd", filemtime($dirArray[$index])); which sets the date as 20080524. and using sort($dirArray, $date2); but this doesnt work. have also tried variations trying to make sense of the php manual for sort(); such as the below but none work and produce errors: sort($dirArray, $date2 SORT_ASC); sort($dirArray $date2, SORT_ASC); what is the correct syntax for this? Thanks, Scott Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2008 Share Posted May 24, 2008 This is incorrect format for the sort() function sort($dirArray, $date2 SORT_ASC); sort($dirArray $date2, SORT_ASC); The sort() function takes only 2 arguments: 1 The array and optionally 2 the sort flag. And which array are you trying to sort? In your first post you talked about the variables $adate and $date2 (one dimensional arrays). But in your example in the last post you are trying to sort $dirArray which is a multi-dimensional array. The sort() method you would use will be different based upon the type of array you are using. for single dimensional arrays sort() would be the proper choice. For multi-dimensional arrays there are different methods based upon the array structure. For what you have I think you will want to use array_multisort(). Look at the instructions for sorting database results in the manual: http://us2.php.net/manual/en/function.array-multisort.php Quote Link to comment Share on other sites More sharing options...
skot Posted May 25, 2008 Author Share Posted May 25, 2008 Thanks but i've been looking at the manual for array_multisort() and i'm not sure how to apply this to my multi-dimensional array. Tried this: // sort by date created $ar = array($dirArray[$index]); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); var_dump($ar); which prints the following on the page: array(2) { [0]=> NULL [1]=> NULL } Basically how do I put in PHP language the following: Sort all entries in array $dirArray[$index] by $date2 (which is a date field in the format 20080525) most recent first Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2008 Share Posted May 25, 2008 I cleaned up a lot of your code, but not all of it. For example, the FONT tag has been deprecated for several years now. Just create style classes instead. Anyways, thsi shoudl be very close to what you are wanting: <?php // open dir $path = '.'; $myDirectory = opendir($path); // fetch entries while($entryName = readdir($myDirectory)) { if (!is_dir($path.'/'.$entryName)) { $dirArray[] = $entryName; $fileDates[] = date('Ymd', filectime($entryName)); } } // close dir closedir($myDirectory); //Sort by date array_multisort($fileDates, $dirArray); echo "<pre>".print_r($dirArray)."</pre>"; echo "<font face=verdana size=3 color=#F8F304><B>"; echo ":: To download right click on a track and 'save as...'</B><br>\n"; echo ":: To upload click the link at the top-right.<br><br>\n"; echo ":: Current Track Count: " . count($dirArray) . "</font>\n"; // print header row echo "<center>\n"; echo "<table width=\"95%\" border=\"1\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#183041\">\n"; echo "<tr align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <th>Name</th>\n"; echo " <th>Type</th>\n"; echo " <th>File Size</th>\n"; echo " <th>Date Added</th>\n"; echo "</tr>\n"; // print results foreach ($dirArray as $file) { $ext = array_pop(explode(".", ($file))); $date = date('d/m/Y', filectime($file)); $sizeinmb = round((filesize($file) / 1024 / 1024), 2); echo "<tr background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <td align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">"; echo "<font face=\"verdana\" size=\"2\" color=\"#D9F4FD\"><b> "; echo "<a href=\"$file\"><img src=\"../img/star.gif\" border=\"0\">"; echo $file . "</a></b></font></td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">$ext</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">$sizeinmb</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">$date</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center><br clear=\"all\">\n"; ?> Quote Link to comment Share on other sites More sharing options...
skot Posted May 25, 2008 Author Share Posted May 25, 2008 Thanks for taking the time to do that - it's much appreciated. I can also learn by looking at the code so thanks again. After a bit of tweaking have managed to get this working. However all files in the directory are currently displayed to users. How would I go about excluding all files that are not mp3 or wma files? This is something I have been looking into for a while but could never get to work.. Thanks, Scott Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 25, 2008 Share Posted May 25, 2008 Ah hell, if you're going to do that then I would go with a different appraoch. You can have the results sorted by whatever you define as the $fileIdx value. <?php //Valid extensions $valid_extensions = array ('mp3', 'php'); $path = '.'; $dirArray = array(); // open dir $myDirectory = opendir($path); // fetch entries while($fileName = readdir($myDirectory)) { //Only process files if (!is_dir($path.'/'.$fileName)) { //Only process files with valid extensions $ext = strtolower(array_pop(explode(".", ($fileName)))); if (in_array($ext, $valid_extensions)) { $fileIdx = date('Ymd', filemtime($fileName)); $dirArray[$fileIdx]['name'] = $fileName; $dirArray[$fileIdx]['ext'] = $ext; $dirArray[$fileIdx]['date'] = date('m/d/Y', filemtime($fileName)); $dirArray[$fileIdx]['size'] = round((filesize($fileName) / 1024 / 1024), 2); } } } // close dir closedir($myDirectory); //Sort by key ksort($dirArray); echo "<font face=verdana size=3 color=#F8F304><B>"; echo ":: To download right click on a track and 'save as...'</B><br>\n"; echo ":: To upload click the link at the top-right.<br><br>\n"; echo ":: Current Track Count: " . count($dirArray) . "</font>\n"; // print header row echo "<center>\n"; echo "<table width=\"95%\" border=\"1\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#183041\">\n"; echo "<tr align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <th>Name</th>\n"; echo " <th>Type</th>\n"; echo " <th>File Size</th>\n"; echo " <th>Date Added</th>\n"; echo "</tr>\n"; // print results foreach ($dirArray as $file) { echo "<tr background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <td align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">"; echo "<font face=\"verdana\" size=\"2\" color=\"#D9F4FD\"><b> "; echo "<a href=\"{$file['name']}\"><img src=\"../img/star.gif\" border=\"0\">"; echo $file['name'] . "</a></b></font></td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['ext']}</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['size']}</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['date']}</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center><br clear=\"all\">\n"; ?> Quote Link to comment Share on other sites More sharing options...
skot Posted May 25, 2008 Author Share Posted May 25, 2008 I've implemented your code amendment, but instead of listing the 414 files with a .mp3 or .wma extension in the directory, now only 27 are listed. //Valid extensions $valid_extensions = array ('mp3', 'wma', 'MP3', 'WMA'); $path = '.'; $dirArray = array(); Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 26, 2008 Share Posted May 26, 2008 Oh, I see. Because I used the file date as the array index, if there are multiple files with the same date they will overwrite one another. give me a few minutes and I'll post a fix. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 26, 2008 Share Posted May 26, 2008 OK, yet another new approach. This time we are going to use usort() which requires a user defined function. Also, you do not need to put upper-case and lower-case values in the valid extensions list. I force the actual extension to lower-case when doing the check. So, just enter the values into the $valid_extensions array in lower case and you are all set. <?php function sortByDate($a, $b) { if ($a['dateSort'] == $b['dateSort']) { return 0; } return ($a['dateSort'] < $b['dateSort'])? -1 : 1; } //Valid extensions $valid_extensions = array ('mp3', 'wma'); $path = '.'; $dirArray = array(); // open dir $myDirectory = opendir($path); // fetch entries while($fileName = readdir($myDirectory)) { //Only process files if (!is_dir($path.'/'.$fileName)) { //Only process files with valid extensions $ext = strtolower(array_pop(explode(".", ($fileName)))); echo $ext; if (in_array($ext, $valid_extensions)) { echo ": True"; $fileIdx = count($dirArray); $dirArray[$fileIdx]['name'] = $fileName; $dirArray[$fileIdx]['ext'] = $ext; $dirArray[$fileIdx]['dateSort'] = date('Ymd', filemtime($fileName)); $dirArray[$fileIdx]['date'] = date('m/d/Y', filemtime($fileName)); $dirArray[$fileIdx]['size'] = round((filesize($fileName) / 1024 / 1024), 2); } echo "<br>"; } } // close dir closedir($myDirectory); //Sort by key usort($dirArray, 'sortByDate'); echo "<font face=verdana size=3 color=#F8F304><B>"; echo ":: To download right click on a track and 'save as...'</B><br>\n"; echo ":: To upload click the link at the top-right.<br><br>\n"; echo ":: Current Track Count: " . count($dirArray) . "</font>\n"; // print header row echo "<center>\n"; echo "<table width=\"95%\" border=\"1\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#183041\">\n"; echo "<tr align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <th>Name</th>\n"; echo " <th>Type</th>\n"; echo " <th>File Size</th>\n"; echo " <th>Date Added</th>\n"; echo "</tr>\n"; // print results foreach ($dirArray as $file) { echo "<tr background=\"http://bridgey.net/img/row_bg.gif\">\n"; echo " <td align=\"left\" background=\"http://bridgey.net/img/row_bg.gif\">"; echo "<font face=\"verdana\" size=\"2\" color=\"#D9F4FD\"><b> "; echo "<a href=\"{$file['name']}\"><img src=\"../img/star.gif\" border=\"0\">"; echo $file['name'] . "</a></b></font></td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['ext']}</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['size']}</td>\n"; echo " <td background=\"http://bridgey.net/img/row_bg.gif\">{$file['date']}</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center><br clear=\"all\">\n"; ?> Quote Link to comment Share on other sites More sharing options...
skot Posted May 26, 2008 Author Share Posted May 26, 2008 Thanks again - works brilliantly Quote Link to comment Share on other sites More sharing options...
skot Posted May 26, 2008 Author Share Posted May 26, 2008 How about counting the total filesize? I have attempted: $totalcount = count($dirArray[$fileIdx]['size']); echo ":: Current Total Size: " . $totalcount . "</font>\n"; But this returns '1'. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 26, 2008 Share Posted May 26, 2008 I just realized I left some debugging code in. Anyway, here is the loop w/o the debugging code and with the necessary step to calculate total size of all files: // fetch entries while($fileName = readdir($myDirectory)) { //Only process files if (!is_dir($path.'/'.$fileName)) { //Only process files with valid extensions $ext = strtolower(array_pop(explode(".", ($fileName)))); if (in_array($ext, $valid_extensions)) { $fileIdx = count($dirArray); $size = filesize($fileName); $totalSize += $size; $dirArray[$fileIdx]['name'] = $fileName; $dirArray[$fileIdx]['ext'] = $ext; $dirArray[$fileIdx]['dateSort'] = date('Ymd', filemtime($fileName)); $dirArray[$fileIdx]['date'] = date('m/d/Y', filemtime($fileName)); $dirArray[$fileIdx]['size'] = round(($size / 1024 / 1024), 2); } } } $totalSize = round(($totalSize/ 1024 / 1024), 2); Quote Link to comment Share on other sites More sharing options...
skot Posted May 26, 2008 Author Share Posted May 26, 2008 Thanks again mjdamato. The finished product is here: www.bridgey.net/music username: scott password: tranceplz In the future I want to get this so that php generates a hidden spry area for each entry in the array, that when clicked users can view detailed information about the file such as bit rate, genre, and song length. I also want to incorporate a 'force download' script so you dont have to right click and save as, but these ideas are a long way off. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 26, 2008 Share Posted May 26, 2008 To get the ID3 info from audio files is easy with the free getID3() class. http://getid3.sourceforge.net/ 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.