Have an account? Sign in
Login  Register  Facebook
How to count files in folder
This is my script which count file size in folder \"files\"
But in folder \"files\" there is subfolder \'screenshot\', how to set up script not to count files in subfolder \'screenshot\'
<?php
$maxupload=1024000000; // in byte
$dir=\'files\';
$notcount=array(\'index.html\',
\'listing.php\',\'descriptionfile.txt\',\'ddimgtooltip.js\',\'Thumbs.db\');
dirs($dir);
$sizeinbyte=($_SESSION[tots]==0)?\'0\':$_SESSION[tots];
$_SESSION[tots]=array();
unset($_SESSION[tots]);
    function dirs($dir){
    global $notcount;
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != \".\" && $file != \"..\") {
                    if(is_dir($dir.\'/\'.$file)){
                        dirs($dir.\'/\'.$file);
                    }else{
                        if (!in_array($file, $notcount)) {
                            $fl=filesize($dir.\'/\'.$file);
                            $_SESSION[tots]=$_SESSION[tots]+$fl;
                        }
                    }
                }
            }
        closedir($handle);
        }
    }
?>
Started: September 16, 2011 Latest Activity: June 13, 2018 files folder count
4 Answers
On Line Erectile Dysfunction Medication <a href=http://cialibuy.com>cialis tablets for sale</a> Cheap Sildenafil Citrate India Support Viagra Sante Canada

Posted: Ellgaibra
In: June 13, 2018

With DirectoryIterator and SplFileInfo
$totalSize = 0;
foreach( new DirectoryIterator('files') as $file) {
    if($file->isFile()) {
        $totalSize += $file->getSize();
    }
};
echo $totalSize;
and in case you need that including subfolders:
$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('files'));

$totalSize = 0;
foreach($iterator  as $file) {
    $totalSize += $file->getSize();
}
echo $totalSize;

Posted: xtremex
In: September 16, 2011

ok the code of @is_set give you the sum of the files of the 'files' folders and it will never count the sub folders is that what are you want?
September 16, 2011

My script does that. My script count size of files in folder "files". I exclude some files so the script doesnt count size of files ('index.html', 'listing.php','descriptionfile.txt','ddimgtooltip.js','Thumbs.db') I don't want to count size of files in files/txt/screenshot (basically any "screenshot" subfolder in folder "files")
September 16, 2011

ok i think you want to get the sum of the \'files\' files ok?
just see this code:
 function sum_size($dir,$ext = false,$ignore = false){
        foreach(array_diff(scandir($dir),array(\'.\',\'..\')) as $file){
            if(is_file($dir.\'/\'.$file) 
                && (($ext) ? ereg($ext.\'$\',$file):1) 
                    && !in_array($file,$ignore)){
                 $size[] = filesize($dir.\'/\'.$file);
            }
        }  
        return array_sum($size);
 }
and use this function to get the size from bytes to kb or mb etc
function bytesToSize($bytes)
{
    $symbols = array(\'B\', \'Kb\', \'Mb\', \'Gb\', \'Tb\', \'Pb\', \'Eb\', \'Zb\', \'Yb\');
    $exp = floor(log($bytes)/log(1024));
 
    return sprintf(\"%.2f \" . $symbols[$exp], ($bytes/pow(1024, floor($exp))));
}
and you will use it as:
$ignore = array(\'README.txt\',
                        \'listing.php\',
                            \'descriptionfile.txt\',
                                \'ddimgtooltip.js\',
                                    \'Thumbs.db\');
echo bytesToSize(sum_size(\'files\',false,$ignore))

Posted: is_set
In: September 16, 2011

ok its very easy just use this php function
function file_list($dir,$ext = false){
       foreach(array_diff(scandir($dir),array(\'.\',\'..\')) as $file){
           if(is_file($dir.\'/\'.$file) && (($ext) ? ereg($ext.\'$\',$file):1)){
                $list[] = $file; 
           }
       }  
       return $list;
}

$dir path without backslash, e.g. \"files\"

$ext optionally filter specific filename extentions, e.g. \".jpg\"

examples get the files names of the files folder
foreach (file_list(\'files\') as $name ) {
    echo $name;	
}
just count them
echo count(file_list(\'files\'));
want get the names of the php files on files only
foreach (file_list(\'files\',\'php\') as $name ) {
    echo $name;	
}

Posted: MacOS
In: September 16, 2011

Thanks, but can you integrate your code into mine, I can't, I don't really know how. And i'm not getting names. I'm counting size of all files in folder and subfolders
September 16, 2011

oh you want get the size of each file in this folder only or want count the sub too?
September 16, 2011

yes i think he want that and i'm going to write it
September 16, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer