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.
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> #'.$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 <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> </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> #'.$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";
}
?>Any suggestions?
Thanks