Numbering directory and subdirectory 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

Numbering directory and subdirectory contents

Post by TildeHash »

Hello again,

How would I go about numbering the contents of a directory and its sub-directories?

This is what I want to do: Directory "test" has files "1.txt", "2.txt", "3.txt", "4.txt", "5.txt", etc and sub-directories "2", and "4" which have similar contents. I'd like a script that generates the following:

[text]test:
1. 1.txt
2. 2.txt
2:
1. 1.txt
2. 2.txt
etc
3. 3.txt
4. 4.txt
4:
1. 1.txt
2. 2.txt
etc
5. 5.txt
etc
[/text]

This would be for my Free Software comment system, so think of the top level files as comment 1, comment 2, etc and the sub-directories as holding the replies files. My goal is to then make a while loop that matches the file counts, and if they don't match up display a message saying the file has been deleted.

Thanks to anyone who can help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Numbering directory and subdirectory contents

Post by requinix »

Nested OLs would do the trick just fine.

Code: Select all

<ol>
  <li>1.txt</li>
  <li>2.txt<ol>
    <li>1.txt</li>
    <li>2.txt</li></ol></li>
  <li>3.txt</li>
  ...
</ol>
  1. 1.txt
  2. 2.txt
    1. 1.txt
    2. 2.txt
  3. 3.txt
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Numbering directory and subdirectory contents

Post by TildeHash »

requinix wrote:Nested OLs would do the trick just fine.

Code: Select all

<ol>
  <li>1.txt</li>
  <li>2.txt<ol>
    <li>1.txt</li>
    <li>2.txt</li></ol></li>
  <li>3.txt</li>
  ...
</ol>
  1. 1.txt
  2. 2.txt
    1. 1.txt
    2. 2.txt
  3. 3.txt
I need it to be done in PHP. So I could do something like this:

Code: Select all

$lastfile = '1';
$lastfile++;
$currentfile = '3';

if ($currentfile > $lastfile) {
   echo $lastfile . ".txt deleted!";
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Numbering directory and subdirectory contents

Post by requinix »

Yeah... So you use PHP to output the right HTML structure.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Numbering directory and subdirectory contents

Post by Weiry »

requinix wrote:Yeah... So you use PHP to output the right HTML structure.
Very helpful there requinix...

TildeHash, i would have a look at using the readdir() function of php which you can use to recursively read through directories for those files.
http://au2.php.net/manual/en/function.readdir.php

This post on that same page has an example of a script which reads through directories and sub directories and converts it into an array.
http://au2.php.net/manual/en/function.r ... php#104824

From there you could easily move it into any format that you want, whether being a html list etc.
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Numbering directory and subdirectory contents

Post by TildeHash »

Well, I got it all figured out, so here is the code for anybody interested:

Code: Select all

<?php
$dir = './comments/comments-html';
$diriter = new RecursiveDirectoryIterator($dir);
$iter = new RecursiveIteratorIterator($diriter, RecursiveIteratorIterator::SELF_FIRST);
$files = array(); $dirs = array();

$dircounts = array();

foreach ($iter as $file) {
	if (is_file($file)) {
		$files[$file->getPathname()] = $file;
		$dirs[$file->getPathname()] = dirname($file);
		foreach ($dirs as $path) {
			$dircounts[str_replace('/..', '', $path)] = '1';
		}
	}
}
uksort($files, 'strnatcasecmp');

foreach ($files as $path => $file) {
	while ($dircounts[dirname($file)] < basename($file, '.txt')) {
		echo 'Deleted!' . "<br>\n";
		$dircounts[dirname($file)]++;
	}
	echo $file . "<br>\n";
	$dircounts[dirname($file)]++;
}
?>
Output:
[text]./comments/comments-html/1.txt
./comments/comments-html/2.txt
./comments/comments-html/3.txt
./comments/comments-html/3/1.txt
Deleted!
./comments/comments-html/3/3.txt
./comments/comments-html/4.txt
Deleted!
./comments/comments-html/6.txt[/text]
Post Reply