I have a problem with a script I am currently developing. I am trying to create a script which reads and lists all the files in a directoy. I have accomplished this. The problem that I am having is that I also want to list the filesize of each file. Unfortunatly the filesize function only allows the output of the size of a single file. I'm using readdir to list the filenames and I was hoping that I could put the output of the directory handle into the filesize function to produce a list of the respective filesizes of all the files in the directory.
Currently I only get the file size of the most recent file.
My code so far is below. some of you may hate it but I am quite new to PHP. Sorry about the messy layout. I'm still learning!
Code: Select all
<?php
// Directory listing script created by Andrew Cain. Copyright 2008.
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="external.js">
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>83 Squadron File Area</title>
</head>
<body>';
$directory = "/home/www/T130dfsE/filearea";
$dir_handle = opendir($directory) or die("Unable to open directory");
function empty_folder_check($directory){
$c=0;
if(is_dir($directory) ){
$files = opendir($directory);
while ($file=readdir($files)){$c++;}
if ($c>2){
return false;
}else{
return true;
}
}
}
function format_size($size, $precision) {
$sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'kB', 'B');
$total = count($sizes);
while($total-- && $size > 1024) $size /= 1024;
return round($size, $precision).$sizes[$total];
}
$check = empty_folder_check($directory);
if ($check == false)
{
echo "\n<div id='bannerdiv'><h1>83 Squadron Digital File</h1></div>";
echo "\n<div id='maincontent'>";
echo "\n<div id='filetable'>";
echo "\n<div id='filename'>";
while($files=readdir($dir_handle))
{
if ($files !="." && $files !="..")
{
echo "\n<p><a href='http://www.83sqn.co.uk/filearea/$files' rel='external'>$files</a></p>";
$output=filesize('A Absent forms.doc');
}
}
}
else {
echo "Empty Directory. No files.";
}
echo "\n</div>";
echo "\n</div>";
echo "\n</div>";
echo "\n<div id='size'>";
$totals=format_size($output,0);
echo $totals;
closedir($dir_handle);
echo "\n</div>";
echo "\n</div>";
echo "\n</body>";
echo "\n</html>";