guscrown Posted October 9, 2010 Share Posted October 9, 2010 Sup PHP Freaks, A few years ago a friend helped me out with a website; he did some PHP coding for me to display a table with directories and files from a folder on my server, so people would be able to download. For years the code worked fine on Windows servers. Recently I had to move to a Linux server and something is not working 100%. The opendir() function is not returning sorted data. I get all the folders but they are sorted in a random matter. I have basically zero knowledge of PHP and I understand the code very little. Can anyone help me figure this out? I believe the code has 2 different sections, one looking for folders, and one looking for actual files. $cat = $_GET["cat"]; $mySortIndex = $_GET["index"]; $myPattern = "/^$mySortIndex/i"; $files = $cat=="" ? 0 : 1; $dir = "/home/guscrown/public_html/BTs/"; $dir = $dir . $cat; $modulehtml = "<table width=80% class=tborder>"; if($files) { $modulehtml .= " <tr> <td class=thead> $cat Backing Tracks </td> <td class=thead width=100px> File Type </td> </tr> "; } else { $modulehtml .= ' <tr> <td class="thead"> Artist Name </td> <td class="thead" width=50px> Tracks </td> </tr> '; } $dh = opendir($dir); $i = 0; $myArray = Array(); while($file = readdir($dh)) { if($file[0] == ".") continue; if(!is_dir($dir . "/" . $file) && !$files) continue; if(is_dir($dir . "/" . $file) && $files) continue; if($files) { $file_parts = explode(".", $file); $file_base = $file_parts[0]; $file_ext = $file_parts[1]; $modulehtml .= " <tr> <td class=alt1> <a href=\"/index.php?page=dl_info&cat=$cat&file=$file_base&ext=$file_ext\">$file_base</a> </td> <td class=alt2> $file_ext </td> </tr> "; } else { $file_count = count(glob($dir . $file . "/*.*")); $myArray[$i] = $file; if ($mySortIndex != NULL) { if (preg_match($myPattern, $file)) { $modulehtml .= " <tr> <td class=alt1> <a href=\"/index.php?page=dl_list&cat=$file\">$file</a> </td> <td class=alt2> $file_count </td> </tr> "; } } else { $modulehtml .= " <tr> <td class=alt1> <a href=\"/index.php?page=dl_list&cat=$file\">$file</a> </td> <td class=alt2> $file_count </td> </tr> "; } } $i++; } closedir($dh); $modulehtml .= "</table>"; //$home["$mods[modid]"]['content'] = $modulehtml; echo $modulehtml; echo $myMessage; ?> Quote Link to comment https://forums.phpfreaks.com/topic/215485-help-with-code-snipet/ Share on other sites More sharing options...
guscrown Posted October 9, 2010 Author Share Posted October 9, 2010 Am I allowed to bump? I just want to catch the morning crowd. Quote Link to comment https://forums.phpfreaks.com/topic/215485-help-with-code-snipet/#findComment-1120577 Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 readdir returns files listed in the same order as the filesystem stores them. The only option is to store the names in an array, sort the array, then display the results. But, this will sort them alphabetically. So alternatively we could use the time the file was made to sort by. while(false !== ($file = readdir($dh))) { if($file != '.' && $file != '..') { if(!is_dir($file)) { $filesArray[filemtime($file)] = $file; } } } if(isset($filesArray)) { ksort($filesArray); } else { die('files don\'t exist'); } foreach($filesArray as $filename) { $file_parts = explode(".", $filename); $file_base = $file_parts[0]; $file_ext = $file_parts[1]; $modulehtml .= "<tr>\n<td class=alt1>\n <a href=\"/index.php?page=dl_info&cat=$cat&file=$file_base&ext=$file_ext\">$file_base</a>\n </td>\n<td class=alt2>\n$file_ext\n</td>\n</tr>\n"; } This is only an example. Quote Link to comment https://forums.phpfreaks.com/topic/215485-help-with-code-snipet/#findComment-1120581 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.