Tuesday, June 23, 2009

How to see directory contents, file size, file type, modification date using php

There is already some raw output of directory contents which is shown in the post List of directory contents using array in php. In this post I will display the directory contents into more readable format as well as the contents size, file type and modification date of the file/folder.

We will start by making a hard code directory in our code to which contents file type, size will be displayed.

1)We assign the directory location to a path so that we can work further with this $path variable.
$path='D:/test';

2)You need to open the directory using php opendir() function.
$myDirectory = opendir($path);

3)Using a while loop read each directory contents and put them into an array variable named $dirArray.
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}

4)As we already read all the contents and the contents are within array variable so now we can close the directory using,
closedir($myDirectory);

5)We need to count the number of elements inside the array, in order words number of files exist inside the operating system directory.
$indexCount = count($dirArray);

6)Optionally we are sorting here so that we can display them in an order.
sort($dirArray);

7)As you have seen in the example http://arjudba.blogspot.com/2009/03/list-of-directory-contents-using-array.html [0] => . [1] => .. are there and you might not want to display those hidden contents. So before displaying contents you will check whether filename starts with a dot (.) . If it starts with a dot file will not be displayed on the browser. Simply using if condition we can omit it.
if (substr("$dirArray[$i]", 0, 1) != ".")

8)To display all files in the array we can use for loop construct.
for($i=0; $i < $indexCount; $i++) Now the above php lines are combined and embedded with html in order to display result in a good format. Full code is below.

<?
$path='D:/test';
// open this directory
$myDirectory = opendir($path);

// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

// count elements in array
$indexCount = count($dirArray);

// sort them
sort($dirArray);
?>

<html>
<body>
<table style="margin-bottom: 1px;" border="0" cellpadding="6" cellspacing="1" width="90%">
<tbody>
<tr>
<th class="TableData" style="padding-left: 11px; padding-right: 1px;"
bgcolor="#dadce1" width="300" height="22">File Name
</th>

<th class="TableData" style="padding-left: 11px; padding-right: 1px;"
bgcolor="#dadce1" width="100" height="22">File Size
</th>

<th class="TableData" style="padding-left: 11px; padding-right: 1px;"
bgcolor="#dadce1" width="350" height="22">Date Modified
</th>

<th class="TableData" style="padding-left: 11px; padding-right: 1px;"
bgcolor="#dadce1" width="110" height="22">File Type
</th>

<?
for($i=0; $i < $indexCount; $i++) {
// don't list hidden files
if (substr("$dirArray[$i]", 0, 1) != "."){

?>

<tr>
<td class="Lebels" align="right" bgcolor="#e9ece9" >
<? echo $dirArray[$i]; ?>
</td>
<?
print("<td bgcolor=\"#e9ece9\">");
# We may want a file size. Note that you need to add path
if( filesize( $path . "/" . $dirArray[$i] ) >= 1024 ) {
# Size in kilobytes
print " " . round( filesize( $path . "/" . $dirArray[$i] ) / 1024, 1 )
. " K<br />\n";
} elseif( filesize( $path . "/" . $dirArray[$i] ) >= 1048576 ) {
# Size in megabytes
print " " . round( filesize( $path . "/" . $dirArray[$i] ) / 1024 / 1024, 1 ) . " M<br />\n";
} else {
# Size in bytes
print " " . filesize( $path . "/" . $dirArray[$i] ) . " bytes<br />\n";
}
print("</td>");
?>

<td class="Values" bgcolor="#e9ece9" ><? echo date ("F d Y H:i:s.", filemtime($path . "/" . $dirArray[$i])); ?>
</td>

<td class="Values" bgcolor="#e9ece9" >
<? print(filetype($path . "/" . $dirArray[$i] ));?>

</td>
</tr>
<?
}
}

print("</TABLE>\n");
?>
</body>
</html>

A sample output in my system of the above output is,
File Name File Size Date Modified File Type
Accounts.txt 297 bytes
December 21 2008 16:26:39. file
Ghor_Kutum_16.wmv 23232.6 K
February 08 2009 11:12:36. file
New Folder 0 bytes
June 23 2009 14:58:47. dir
SPFILEORCL.ORA 3.5 K
May 16 2009 06:26:53. file
a.txt 51 bytes
May 16 2009 06:16:28. file
database_dump.txt 10.9 K
June 19 2009 11:09:30. file
database_testprotege_profile.csv 1.7 K
June 20 2009 16:50:32. file
zwt-escep310.rar 1089.7 K
December 13 2008 08:38:16. file


Related Documents

No comments:

Post a Comment