Page 1 of 1
Numbering directory and subdirectory contents
Posted: Wed May 02, 2012 4:31 am
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.
Re: Numbering directory and subdirectory contents
Posted: Wed May 02, 2012 4:47 am
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.txt
- 2.txt
- 1.txt
- 2.txt
- 3.txt
Re: Numbering directory and subdirectory contents
Posted: Wed May 02, 2012 5:05 am
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.txt
- 2.txt
- 1.txt
- 2.txt
- 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!";
}
Re: Numbering directory and subdirectory contents
Posted: Wed May 02, 2012 10:30 am
by requinix
Yeah... So you use PHP to output the right HTML structure.
Re: Numbering directory and subdirectory contents
Posted: Wed May 02, 2012 9:33 pm
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.
Re: Numbering directory and subdirectory contents
Posted: Thu May 03, 2012 7:06 am
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]