rewinddir() function

The rewinddir() function resets PHP's internal pointer to the beginning of a directory listing.
It returns true (1) unless an error occurs, in which case it returns false (0).

Our code prints out the file names first, in a continuous series of <TD>'s in one <TR>. When the last file in the directory is read, the rewinddir() function is run, we rewind to the first file in directory and then print out the filesizes directly below the corresponding filenames in a new <TR>.

BEGIN PHP output

Filename . .. lunchForm.html rewinddir.php file7-6.txt translateArray.php writeTable.php getLastMod.php fgets.php data.txt file7_7.txt elseifTest.php lastLogClass.php stat.php opendir.php file.php phpinfo.php recursion.php whileTest.php popen_pclose.php getCurrentUser.php fileIO.php closedir.php lunchCost.php
Size 671 346 682 2998 189 2266 1750 2597 1655 258 9098 2461 2416 2772 3105 1176 1045 2115 2076 1885 898 2387 1739 782


End PHP output

Back to Bill's PHP

Using the <pre> tag, we show the code below which produces the results shown above. The code you see below is also run "behind the scenes" in PHP, producing the table (above)


<?

/*
/** print the current directory in a table
*/

print("<table border=\"1\">\n");

/*
/** open directory
*/
$myDirectory = opendir(".");

print("<tr>\n");
print("<th bgcolor=bluegreen>Filename</th>\n");

// Get each entry
while($entryName = readdir($myDirectory)) {

    print("<td bgcolor=seagreen>$entryName</td>\n");
}
print("</tr>\n");

// go back to the begining
rewinddir($myDirectory);

print("<tr>\n");
print("<th bgcolor=yellow>Size</th>\n");

// Get each entry
while($entryName = readdir($myDirectory)) {

     print("<td bgcolor=orange align=\"right\">\n");
     print(filesize($entryName));
     print("</td>\n");
}

/*
/** Close directory
*/

closedir($myDirectory);
 print("</tr>\n");
 print("</table>\n");
 
?>
<pre>

Back to Bill's PHP