Nandini Posted October 17, 2008 Share Posted October 17, 2008 Hi I am displaying files from on directory. Everything working. But i want to display latest added records as first record (Last come first in order). Here is my script . <?php $path="/usr/local/netz/var/spool/netz/fax/"; if($path) { if(`ls -A $path`) { $i=1; $TrackDir=opendir($path); while ($file = readdir($TrackDir)) { if ($file == "." || $file == "..") { } elseif(preg_match('/\b.pdf\b/', $file)) { echo "<tr bgcolor='#acbdee'>"; echo "<td class='data' width='10%'>$i</td>"; echo "<td class='data' width='30%'>".$file."</td>"; echo '<td class="data" width="30%" align="center"><table border="0" width="50%"><tr><td align="right"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><a href="fax_download.php?download_file='.$file.'" class="fax_links" title="Download"><img src="../images/download.gif" border="0"></a></font></td><td align="right"><a href="delete_fax.php?file='.$file.'" class="fax_links" title="Delete"><img src="../images/delete.gif" border="0"></a></td></tr></table></td>'; echo "<tr>"; $i++; } } closedir($TrackDir); } else { echo "<br><center><b>No records found</b></center>"; } } ?> Can anyone help me Link to comment https://forums.phpfreaks.com/topic/128809-displaying-files/ Share on other sites More sharing options...
JasonLewis Posted October 17, 2008 Share Posted October 17, 2008 You'd need to get the modification time or something using a function like filemtime() then add them into an array then sort that array and display the files. Link to comment https://forums.phpfreaks.com/topic/128809-displaying-files/#findComment-667805 Share on other sites More sharing options...
Nandini Posted October 17, 2008 Author Share Posted October 17, 2008 When file modified wecan get filetime by using filemtime. But when file add how can we get time. Link to comment https://forums.phpfreaks.com/topic/128809-displaying-files/#findComment-667812 Share on other sites More sharing options...
ghostdog74 Posted October 17, 2008 Share Posted October 17, 2008 why do you need to call the shell's ls command? anyway, here's another way $path = realpath('/path/test'); $objects = new DirectoryIterator($path); foreach($objects as $name => $object){ if ( $objects->isFile($name) && substr($object,-4,4 )==".txt" ) { $records[$object->getMTime()]=$object->getFileName();; } } ksort($records); print_r($records); Link to comment https://forums.phpfreaks.com/topic/128809-displaying-files/#findComment-667814 Share on other sites More sharing options...
JasonLewis Posted October 17, 2008 Share Posted October 17, 2008 You can't get the creation time of a file as far as I know. filemtime() should be able to give you the time of the files creation anyway. Try it, it can't hurt. Link to comment https://forums.phpfreaks.com/topic/128809-displaying-files/#findComment-667816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.