Dysan Posted February 17, 2008 Share Posted February 17, 2008 Hi, Using PHP, is it possible to display the file size of a folder on a web page? Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/ Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 Yes. However you'll have to code the function yourself, there is not a builtin function for this. You'll want to read up on opendir and readdir functions. Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-468777 Share on other sites More sharing options...
Lumio Posted February 17, 2008 Share Posted February 17, 2008 I made a script for that: <?php /* author: Benjamin Falk mail: falk [at] citrosaft.com you can use this script, change and copy it, as you wish. */ global $humanity, $verbose; $argv = $_SERVER['argv']; $dir = ''; $humanity = false; $verbose = false; $help = false; foreach ($argv as $a) { //read arguments if (is_file($a)) continue; elseif ($a == '--help') $help = true; elseif (substr($a, 0,1) == '-') { $o = str_split(substr($a, 1)); // split arguments foreach ($o as $i) { if ($i == 'h') $humanity = true; elseif ($i == 'v') $verbose = true; elseif ($i == '?') $help = true; else die("Unknown argument $i\n"); } }elseif (is_dir($a) && file_exists($a)) $dir = $a; } /* Help-Section */ if ($help === true) { echo "Usage: php ".basename(__FILE__)." [OPTION]... [DIRECTORY]\n"; echo "Scanns directories and outputs the total size and total numbers of directories and files.\n\n"; echo " -v\t\tVerbose mode. Shows you which directory get scanned right now.\n"; echo " -h\t\tThe result gets formated better\n"; echo " -?, --help\tOuputs this text\n\n"; exit; } /* end of Help-Section */ if ($dir == '') $dir = dirname(__FILE__); echo "Reading $dir...\n"; $res = getSize($dir); //start scanning if ($humanity === true) { $size = $res[0]; $e = array('Bytes', 'KB', 'MB', 'GB', 'TB'); $i=0; while ($size >= 1024 && $i < count($e)) { $i++; $size /= 1024; } $size = round($size, 2); $fn1 = 'Size'; $fn2 = 'Directories'; $fn3 = 'Files'; $margin = 5; $f1 = strlen(strval($size).' '.$e[$i]); if ($f1 < strlen($fn1)) $f1 = strlen($fn1); $f2 = strlen(strval($res[1])); if ($f2 < strlen($fn2)) $f2 = strlen($fn2); $f3 = strlen(strval($res[2])); if ($f3 < strlen($fn3)) $f3 = strlen($fn3); echo str_pad($fn1,$f1,' ',STR_PAD_LEFT). str_repeat(' ',$margin). str_pad($fn2,$f2,' ',STR_PAD_LEFT). str_repeat(' ',$margin). str_pad($fn3,$f3,' ',STR_PAD_LEFT)."\n"; echo str_pad(strval($size).' '.$e[$i],$f1,' ',STR_PAD_LEFT). str_repeat(' ',$margin). str_pad(strval($res[1]),$f2,' ',STR_PAD_LEFT). str_repeat(' ',$margin). str_pad(strval($res[2]),$f3,' ',STR_PAD_LEFT)."\n"; }else echo "Size\t\tDirectories\t\tFiles\n{$res[0]}\t\t{$res[1]}\t\t{$res[2]}\n"; /* Main-Function */ function getSize($dir) { global $verbose; $cur_size = 0; $cur_files =0; $cur_dirs = 0; if (substr($dir,-1) != '/') $dir .= '/'; $dir .= '*'; $d = glob($dir); foreach ($d as $item) { if (is_dir($item)) { $cur_dirs++; if ($verbose === true) echo "Reading $item...\n"; $res = getSize($item); $cur_size += $res[0]; $cur_dirs += $res[1]; $cur_files += $res[2]; if ($verbose === true) echo " Result of $item:\n". " Size: {$res[0]}\tDirectories: {$res[1]}\tFiles: {$res[2]}\n"; }else { $cur_files++; $cur_size += filesize($item); } } return array($cur_size, $cur_dirs, $cur_files); } /* end of Main-Function */ ?> I wrote it for the php-commandline, but you can change some lines and then you can use it for your webstuff. For your use the function getSize($dir) is important. Just call the function like $dirres = getSize('/var/www'); It returns an array and it is buildt up as Array { [0] Size in bytes [1] Count of directories [2] Count of files } Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-468780 Share on other sites More sharing options...
trq Posted February 17, 2008 Share Posted February 17, 2008 You could use disk_total_space(). Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-468795 Share on other sites More sharing options...
Dysan Posted February 19, 2008 Author Share Posted February 19, 2008 Is it possible to find a display the size of a folder contained in the HTDOCS Folder? <?php // $df contains the total number of bytes available on "/" $df = disk_total_space("/"); // On Windows: disk_total_space("C:"); disk_total_space("D:"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-470641 Share on other sites More sharing options...
wildteen88 Posted February 19, 2008 Share Posted February 19, 2008 Umm, that function will return the disk/partition size the directory is located on. It doesn't calculate the size of the directory. Looking through my old scripts I found this: <?php /* Disclamper: The following function was taken from http://uk3.php.net/manual/en/function.disk-total-space.php#75971 */ function getSymbolByQuantity($bytes) { $symbols = array('Bytes', 'KB', 'MB', 'GB'); $exp = floor(log($bytes)/log(1024)); return sprintf('%.2f '.$symbols[$exp], ($bytes/pow(1024, floor($exp)))); } /* End Disclaimer */ function folder_size($path='.', $dirSize=0) { if(!is_dir($path)) die('Path (' . $path . ') is invalid'); $handle = opendir($path); $ignoreFiles = array('.', '..'); while ( false !== ($file = readdir($handle))) { if (!in_array($file, $ignoreFiles)) { $filePath = $path . '/' . $file; if(is_dir($filePath)) { $dirSize = folder_size($filePath, $dirSize); } else { $dirSize += filesize($filePath); } } } closedir($handle); return $dirSize; } $folder_size = getSymbolByQuantity(folder_size('.')); echo 'Total folder size = ' . $folder_size; ?> Note: getSymbolByQuantity function is not mine. Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-470851 Share on other sites More sharing options...
Dysan Posted February 19, 2008 Author Share Posted February 19, 2008 Using the opendir() function, how do I open a directory and loop through the files, and sum the size of all the files? Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-470903 Share on other sites More sharing options...
trq Posted February 19, 2008 Share Posted February 19, 2008 Using the opendir() function, how do I open a directory and loop through the files, and sum the size of all the files? Thats pretty much what wildteens function does. Quote Link to comment https://forums.phpfreaks.com/topic/91501-folder-size/#findComment-471027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.