There must be a simple answer to this, but I'm a noob so go easy on me :-)
While trying to write a simple routine that displays the contents of a directory,
something like:
while (($file = readdir($dh)) !== false) {
if ( is_dir($file)) echo "d";
if (!is_dir($file)) echo "f";
echo ": $file<br>";
}
I found I couldn't get the routine to recognize a directory entry. So I started playing around with $stat(), and discovered I couldn't pull out stats on any file (except for . and ..). I don't understand why.
I'm running under a locally-installed LAMP: Ubuntu 10.4, ext3, latest Apache2 2.2.14-5ubuntu8 (amd64), PHP 5.3.2-1ubuntu4.
The directory:
nathanael@nathanael-laptop:~/www/accardi/canon/AMFV$ ls -l -a
total 104
drwxr-xr-x 5 nathanael nathanael 4096 2010-05-05 10:23 .
drwxr-xr-x 38 nathanael nathanael 4096 2010-05-05 10:23 ..
-rwxrwxrwx 1 nathanael nathanael 33095 2010-03-05 19:26 AMFV.jpg
-rwxrwxrwx 1 nathanael nathanael 15515 2010-05-05 10:13 AMFV.php
-rwxrwxrwx 1 nathanael nathanael 15491 2010-05-05 10:13 AMFV.php~
-rwxrwxrwx 1 nathanael nathanael 14299 2010-03-05 19:26 AMFV.php.original
drwxr-xr-x 2 nathanael nathanael 4096 2010-05-05 10:23 Downloads
drwxr-xr-x 2 nathanael nathanael 4096 2010-05-05 10:23 Game
drwxr-xr-x 3 nathanael nathanael 4096 2010-05-05 10:23 Greybox
The code:
clearstatcache();
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "<b>$file</b> - ";
$stat = stat($file);
if (!$stat):
echo "No stats";
else:
for ($i = 0; $i < 13; $i++) { echo $i . ") " . $stat[$i]. " "; }
endif;
echo "<br>"; } // end while
closedir($dh); } // end if ($dh)
The output:
/home/nathanael/www/accardi/canon/AMFV
AMFV.php - No stats
Game - No stats
.. - 0) 2055 1) 537518 2) 16877 3) 6 4) 1000 5) 1000 6) 0 7) 4096 8) 1273026234 9) 1273026194 10) 1273026194 11) 4096 12) 8
AMFV.jpg - No stats
AMFV.php~ - No stats
AMFV.php.original - No stats
Greybox - No stats
Downloads - No stats
. - 0) 2055 1) 537519 2) 16877 3) 38 4) 1000 5) 1000 6) 0 7) 4096 8) 1273026324 9) 1273026194 10) 1273026194 11) 4096 12) 8