Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

sorting a directory listing w/ php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

sorting a directory listing w/ php

Postby QuietStorm » Sun Sep 14, 2003 12:19 pm

i'm working in a simple event list that reads a directory and outputs the content of the txt files in it. So far here's the code :
Code: Select all
<?php
$date = date("Ymd");

$handle=opendir('eventos');
while (($file = readdir($handle))!==false) {

    if ($file >= $date) {
       echo('<table width="100%" style="border: #000000 1px ridge"><tr><td>');
      include("eventos/$file");
      echo('</td></tr></table><br>');
    }
   echo '<br>';
   if ($file <= $date) {
   echo('<table width="100%" style="border: #000000 1px ridge"><tr><td>');
      include("eventos/$file");
      echo('</td></tr></table><br>');
   }
}
closedir($handle);
?>


you can see it at work here.

I get the following error because of the dots that show up on top of the c:\>dir output.

Warning: Failed opening 'eventos/.' for inclusion (include_path='.;D:/phpdev/php/includes;D:/phpdev/php/class') in d:\phpdev\www\public\alpha2\ae\content\agenda\iframe.php on line 22

how can I solve this ? Also, is there any way to sort the files after the 1st if statement has passed ?
QuietStorm
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sat Aug 23, 2003 4:07 pm

Re: sorting a directory listing w/ php

Postby swirlee » Sun Sep 14, 2003 3:54 pm

QuietStorm wrote:how can I solve this ? Also, is there any way to sort the files after the 1st if statement has passed ?


Just use a conditional to ignore files whose names are '.' and '..'.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby QuietStorm » Sun Sep 14, 2003 5:47 pm

thanks, i fixed it :)
now all i need is to find a way to sort the files chronologicaly, just to make the result a little bit more user friendly.
QuietStorm
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sat Aug 23, 2003 4:07 pm

sorting

Postby ruturajv » Sun Sep 14, 2003 9:24 pm

sorting is fairly simple,
get all the filenames into an array,
Sort the array,
display the array

You will have a sorted listing of files
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby Joel » Sun Sep 14, 2003 11:14 pm

This is some code I wrote a while ago, you can easily adapt it to sort by date and time using the array function sort(). filetime($file) returns a timestamp, which is what you can sort by.

Code: Select all
//define the path as relative
  $path = ".";

  //using the opendir function
  $dir_handle = @opendir($path) or die("Unable to open $path");
 
  //make string with 70 spaces
  $space="                                 ";
 
  echo "<PRE>";
  //running the while loop
 
  while ($file = readdir($dir_handle)) {
       
        if ($file != '.' && $file != '..') {
         // filename - output left-justified
         $namext = explode('.', $file);
         if (count($namext) < 2) {
            $files[] = array("name" => $file, "size" => filesize($file)/1024, "datetime" => date("d.M Y H:i:s", filemtime($file)), "folder" => true);
         } else {
            $files[] = array("name" => $file, "size" => filesize($file)/1024, "datetime" => date("d.M Y H:i:s", filemtime($file)));
         }
     }
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby QuietStorm » Mon Sep 15, 2003 11:43 am

thanks for everything guys :)

Also, the files seem to get sorted good enough without an array, but if i come across the same problem again i'll know how to fix it.

the final code is now something like this :
Code: Select all
<?php $date = date("Ymd");
$handle=opendir("content/agenda/eventos");
while (($file = readdir($handle))!==false) {
if($file>$date.".txt"){
   echo('<table width="90%" align="center" style="border: #000000 1px ridge"><tr><td>');
   include("eventos/$file");
   echo('</td></tr></table><br>');
}   
}
closedir($handle);
?>
QuietStorm
New php-forum User
New php-forum User
 
Posts: 9
Joined: Sat Aug 23, 2003 4:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron