Recursively listing numerical directory contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Recursively listing numerical directory contents

Post by TildeHash »

Hello,

I'm having a hard time getting some code to recursively list a directory's numerical contents. What the script is supposed to do is read a directory's contents (contents like 1.txt, 2.txt, 2/, 2/1.txt, 2/2.txt), sort them numerically, then display each file's contents in order. The problem is that if two directories don't have both a 1.txt and a '1' directory, the script just stops reading the contents. :banghead:

Here's the code:

Code: Select all

<?php
function read_comments($dir){
 global $commentcount,$httpreferer;
 $commentcount="1";
 $deletedcount="1";
 if(is_dir($dir)){
  if($dh=opendir($dir)){
   $commentfile=array();
   while($commentfile[]=readdir($dh)){
    if($commentfile=="." || $commentfile=="..")continue;
     sort($commentfile, SORT_NUMERIC);
    }
    closedir($dh);
    foreach ($commentfile as $file){
     if(is_file("$dir/$file")){
      $permalink=explode('/', "$dir/$file", 4);
      $replydir=$dir."/".basename("$dir/$file", ".txt");
      // Check for deleted comment, post a note...
      if(basename("$dir/$file", ".txt")!=$deletedcount){
       echo 'document.write("<br><div id=\"rounddiv\">\n<b style=\"font-size:18px;\"><i>&nbsp;#'.$deletedcount.' Comment Deleted!</i></b>\n");'."\n";
       echo 'document.write("</div>\n");'."\n";
       $commentcount++;
       $deletedcount++;
      }
      echo 'document.write("<form name=\"comment_form2\" action=\"'.$_SERVER["PHP_SELF"].'\" method=\"post\">\n");'."\n";
      echo 'document.write("<br><div id=\"rounddiv\">\n&nbsp;<span style=\"font-size:18px;\"><a href=\"'.$httpreferer.'#'.str_replace(array(".txt", "/"), "c", $permalink[3]).'\" name=\"'.str_replace(array(".txt", "/"), "c", $permalink[3]).'\" title=\"Permalink\">#'.basename("$dir/$file", ".txt").'</a>&nbsp;</span>");'."\n";
      readfile("$dir/$file");
      echo 'document.write("<input type=\"hidden\" name=\"deletecomment\" value=\"'.$permalink[3].'\">\n");';
      echo 'document.write("<br><br><div align=\"right\"><span id=\"reply'.str_replace(".txt", "", $permalink[3]).'\"><a href=\"#\" onclick=\"reply\(\''.str_replace(".txt", "", $permalink[3]).'\'\); return false;\">Reply To Comment</a></span>\n");'."\n";
      echo 'document.write("<span id=\"deletecomment'.str_replace(".txt", "", $permalink[3]).'\">- <a href=\"#\" onclick=\"deletecomment\(\''.str_replace(".txt", "", $permalink[3]).'\'\); return false;\">Delete</a></span></div>\n");'."\n";
      // Check for replies, display as necessary...
      echo 'document.write("</div>\n</form>\n");'."\n";
      $commentcount++;
      $deletedcount++;
     }else if(is_dir($replydir)){
       // Check for a reply directory belonging to a deleted comment
       $missingcomment=basename("$file", ".txt");
       $missingcomment++;
       if(is_dir($dir."/".$missingcomment) && !file_exists($dir."/".$missingcomment.".txt")){
        $deletedcount++;
        echo 'document.write("</div>\n</form>\n");'."\n";
        echo 'document.write("<br><div id=\"rounddiv\">\n<b style=\"font-size:18px;\"><i>&nbsp;#'.$deletedcount.' Comment Deleted!</i></b></div>\n");'."\n";
        echo 'document.write("<div style=\"padding:0px 0px 0px 20px;\">\n");'."\n";
        read_comments($dir."/".$missingcomment);
        $commentcount="1";
       }
       echo 'document.write("<div style=\"padding:0px 0px 0px 20px;\">\n");'."\n";
       read_comments($replydir);
       echo 'document.write("</div>\n</form>\n");'."\n";
       $commentcount="1";
     }
    }
  }
 }
}
read_comments($dir);

if($totalcount=="1"){
 echo 'document.write("<br><div id=\"rounddiv\"><b>Be the first to comment!</b></div>\n");'."\n";
}
?>
It's some convoluted code, I know, I hope it's not confusing :lol:

Any suggestions?

Thanks :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Recursively listing numerical directory contents

Post by requinix »

Indentation is great, but a whole one space per level isn't really all that helpful.

Are you using PHP 5?

Code: Select all

$diriter = new RecursiveDirectoryIterator($dir);
$iter = new RecursiveIteratorIterator($diriter, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iter as $file) {
    if ($file->isFile()) {
        // print the comments in this file
        // note that $file is an SplFileInfo class, not a path string
    }
}
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Recursively listing numerical directory contents

Post by TildeHash »

tasairis wrote:Indentation is great, but a whole one space per level isn't really all that helpful.
How you mean?
tasairis wrote:Are you using PHP 5?
Yes I am, and holy cow! I need to learn more PHP 5. How do I get it to sort correctly?

I'm getting this:
6.txt
4.txt
3.txt
5.txt
6/1.txt
2.txt
4/2.txt
4/1.txt
1.txt

Ideally, I'd like it like this:
1.txt
2.txt
3.txt
4/1.txt
4/2.txt
4.txt
5.txt
6/1.txt
6.txt
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Recursively listing numerical directory contents

Post by requinix »

Indentation helps a lot with skimming over code: checking that stuff lines up, looking for loops, etc. But one single space (unless it's in a large font) is too close to no indentation. Can be quite hard to skim over code because you have to look harder to see if a line is indented further than the one above.
But whatever.

I thought it did sort. Oh well.

Code: Select all

$diriter = new RecursiveDirectoryIterator($dir);
$iter = new RecursiveIteratorIterator($diriter, RecursiveIteratorIterator::SELF_FIRST);

$files = array();
foreach ($iter as $file) {
    if ($file->isFile()) $files[$file->getPathname()] = $file;
}
ksort($files, SORT_NUMERIC);

foreach ($files as $path => $file) {
    // print the comments in this file
}
I'm not sure if SORT_NUMERIC will make it sort correctly.

Are you using 5.3?
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Recursively listing numerical directory contents

Post by TildeHash »

tasairis wrote:Indentation helps a lot with skimming over code: checking that stuff lines up, looking for loops, etc. But one single space (unless it's in a large font) is too close to no indentation. Can be quite hard to skim over code because you have to look harder to see if a line is indented further than the one above.
But whatever.
Oh, okay I'll be sure to use tabs from now on.
tasairis wrote:I'm not sure if SORT_NUMERIC will make it sort correctly.
Yeaaahh...

4/2.txt
4/1.txt
1.txt
2.txt
6/1.txt
4.txt
3.txt
5.txt
6.txt

:lol:
tasairis wrote: Are you using 5.3?
I believe so, I'm using whatever is newest in Debian Squeeze.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Recursively listing numerical directory contents

Post by requinix »

5.3.3, apparently.

Code: Select all

uksort($files, "strnatcasecmp");
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Recursively listing numerical directory contents

Post by TildeHash »

tasairis wrote:5.3.3, apparently.

Code: Select all

uksort($files, "strnatcasecmp");
Yep! That works great! I've got a couple things to adjust now, but other than that it's perfect!

(This is what I'm working on by the way: http://www.tildehash.com/comments.html)
Post Reply