how to echo the number of files in a specific folder using PHP ? or the total size of all the hosted files..

Posted on 2008-06-13
Last Modified: 2010-04-16
I'm running a file hosting service and i'd like my visitors to see how many files are hosted ... or the total size of the hosted files using PHP ... i'm a total beginner at PHP and i have no idea how 2 do that ... if anyone could help me out i'd really appreciate it

Question by:nkrulez
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 17

Expert Comment

ID: 21781599
Here is a class that does this:

have you used classes before?

Author Comment

ID: 21781728
i'm afraid not

Author Comment

ID: 21781969
i kind of figured out how 2 get the number of files ... now i just need to know how to get the total size of the files in a specific directory ....

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

LVL 17

Expert Comment

ID: 21782309
can you post your code for the number of files, so we can just build on it?

Author Comment

ID: 21782629
 $path = 'folder/';
print count(glob($path . '*.*'))

simple as that :)
LVL 17

Accepted Solution

nanharbison earned 500 total points
ID: 21782953
well, I can't build on that, I was thinking you were outputting the name of each file in the folder.
Try this, there are two functions, one returns the size of the folder, and the other makes the answer readable, instead of something like 322993445 bytes, it gives the answers in appropriate units, kb, mb, etc
function foldersize($path) {
    $total_size = 0;
    $files = scandir($path);
    foreach($files as $t) {
        if (is_dir($t)) { // In case of folder
            if ($t<>"." && $t<>"..") { // Exclude self and parent folder
                $size = foldersize($path . "/" . $t);
                // print("Dir - $path/$t = $size<br>\n");
                $total_size += $size;
        else { // In case of file
            $size = filesize($path . "/" . $t);
            // print("File - $path/$t = $size<br>\n");
            $total_size += $size;
    return $total_size;
function size_readable ($size, $retstring = null) {
        // gives folder size in a readable way
        $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        if ($retstring === null) { $retstring = '%01.2f %s'; }
        $lastsizestring = end($sizes);
        foreach ($sizes as $sizestring) {
                if ($size < 1024) { break; }
                if ($sizestring != $lastsizestring) { $size /= 1024; }
        if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
        return sprintf($retstring, $size, $sizestring);
 $totalfoldersize = foldersize('put the actual path to the folder in here');
 $results = size_readable($totalfoldersize);
 echo $results;

Open in new window


Author Comment

ID: 21784609
it works!! thanks alot

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Power Action 2 46
Why am I getting SyntaxError: missing ; before statement error 39 81
How to check if the Cron job is running or not ? 12 87
SSL unsecure page mystery 17 47
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question