leequalls Posted September 9, 2010 Share Posted September 9, 2010 Right now I am displaying a list of files in a dir. the files do not output in any type of order I would like them to display either alphabetically or by upload date. I have posted my code below any help would be great. <table border=0><tr><td align=center><b>File</b></td><td><b>Remove</b></td></tr> <?php $remove = $_GET[remove]; $dir = "/home/public_html/upload/files/"; $remove = $dir."".$remove; if($remove != "/home/public_html/upload/files/") {unlink($remove);} // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { $color = "gray"; while (($file = readdir($dh)) !== false) { if($color == "gray") { $color = "black"; } elseif($color == "black") { $color = "gray"; } if(filetype($dir . $file) == "file") { echo "<tr bgcolor=$color><td><a href='../upload/files/$file'>$file</a></td><td align=center><a href='?remove=$file'>X</a></td></tr>"; } } closedir($dh); } } ?> </table> Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/ Share on other sites More sharing options...
wildteen88 Posted September 9, 2010 Share Posted September 9, 2010 To sort them alphabetically you'll need to first need to add all the files to an array, then use asort to sort them into alphabetical order. To output the files you'll use a foreach loop to iterate through the array of files. <?php $remove = $_GET[remove]; $dir = "/home/public_html/upload/files/"; $remove = $dir."".$remove; if($remove != "/home/public_html/upload/files/") {unlink($remove);} // Open a known directory, and proceed to read its contents if (is_dir($dir)){ if ($dh = opendir($dir)) { $files = array(); while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == "file") { // add the file to the files array $files[] = $file; } } closedir($dh); } } // sort the files alphabetically asort($files); ?> <table border=0> <tr> <th>File</th> <th>Remove</th> </tr> <?php // Output the files $i = -1; foreach($files as $file): $color = (++$i % 2 == 0): 'grey' : 'black'; ?> <tr bgcolor="<?php echo $color; ?>"> <td><a href="../upload/files/<?php echo $file; ?>"><?php echo $file; ?></a></td> <td align=center><a href="?remove=<?php echo $file; ?>">X</a></td> </tr> <?php endforeach; ?> </table> I would like them to display either alphabetically or by upload date. You may want to look at the filemtime function Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109121 Share on other sites More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 Hi all, Please correct my if I am wrong, but if error_reporting(E_ALL) was used in this instance, it would highlight an error on the use of $_GET[remove], wouldn't it? (Shouldn't it be $_GET['remove']) It would error with something like " undefined index remove presumed constant on line x" Only reason I ask is because I have always understood that referencing an global ($_POST/$_GET/$_FILES etc) you need to use quotes so that the parser doesn't think as it is handling a constant? Just wondering there... Cheers, Rw Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109124 Share on other sites More sharing options...
wildteen88 Posted September 9, 2010 Share Posted September 9, 2010 rwwd. Yes you're right. I was going to correct those top four lines, but didn't Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109129 Share on other sites More sharing options...
leequalls Posted September 10, 2010 Author Share Posted September 10, 2010 Thanks the files were sorted ok but I got this error Parse error: syntax error, unexpected ':' in /home/party/public_html/en/djpanel/admin/filemanager.php on line 41 $color = (++$i % 2 == 0): 'grey' : 'black'; Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109765 Share on other sites More sharing options...
wildteen88 Posted September 10, 2010 Share Posted September 10, 2010 Change line41 to $color = (++$i % 2 == 0)? 'grey' : 'black'; Link to comment https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109786 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.