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.
Numbering directory and subdirectory contents
Moderator: General Moderators
Re: Numbering directory and subdirectory contents
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.txt
- 2.txt
- 1.txt
- 2.txt
- 3.txt
- TildeHash
- Forum Commoner
- Posts: 43
- Joined: Fri Jul 16, 2010 7:17 am
- Location: Apple Valley, California
Re: Numbering directory and subdirectory contents
I need it to be done in PHP. So I could do something like this: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.txt
- 2.txt
- 1.txt
- 2.txt
- 3.txt
Code: Select all
$lastfile = '1';
$lastfile++;
$currentfile = '3';
if ($currentfile > $lastfile) {
echo $lastfile . ".txt deleted!";
}Re: Numbering directory and subdirectory contents
Yeah... So you use PHP to output the right HTML structure.
Re: Numbering directory and subdirectory contents
Very helpful there requinix...requinix wrote:Yeah... So you use PHP to output the right HTML structure.
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.
- TildeHash
- Forum Commoner
- Posts: 43
- Joined: Fri Jul 16, 2010 7:17 am
- Location: Apple Valley, California
Re: Numbering directory and subdirectory contents
Well, I got it all figured out, so here is the code for anybody interested:
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]
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)]++;
}
?>[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]