Page 1 of 1

Obtaining file sizes from directory

Posted: Thu Jul 31, 2008 5:27 pm
by andycain
Hello.

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>";
 
Thanks guys.

Re: Obtaining file sizes from directory

Posted: Fri Aug 01, 2008 1:48 pm
by feyd
replace your call to filesize() with

Code: Select all

$output += filesize($files);
Make sure $output is initialized to zero outside of the while() loop.

Re: Obtaining file sizes from directory

Posted: Fri Aug 01, 2008 3:04 pm
by VladSun
//offtopic

Welcome back feyd!
Hope you have your PhD degree now :)

Re: Obtaining file sizes from directory

Posted: Fri Aug 01, 2008 5:27 pm
by feyd
VladSun wrote://offtopic

Welcome back feyd!
Hope you have your PhD degree now :)
Not even close.

But I am getting four bachelor degrees. :) If people want to continue this, let's move it elsewhere.

Re: Obtaining file sizes from directory

Posted: Sun Aug 03, 2008 7:27 am
by andycain
feyd wrote:replace your call to filesize() with

Code: Select all

$output += filesize($files);
Make sure $output is initialized to zero outside of the while() loop.
Sorry I don't think I made myself quite clear in my first post (i've just edited it).

I want the script to list the filenames (which it does allready) but also the individual filesizes of each file.

Eg...

File 1 - File Size of File 1

File 2 - File size of File 2

If I use the filesize function it wil simply return the filesize of the file highest in the directory. This is because I am using readdir from a directory handle.

Hopefully this is a bit more clear. Appreciate your help Feyd.