Solved

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
7
180 Views
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

thanks.
0
Comment
Question by:nkrulez
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
Here is a class that does this:
http://tnt.goldnet.ca/tree.phps


have you used classes before?
0
 

Author Comment

by:nkrulez
Comment Utility
i'm afraid not
0
 

Author Comment

by:nkrulez
Comment Utility
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 ....

thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
can you post your code for the number of files, so we can just build on it?
0
 

Author Comment

by:nkrulez
Comment Utility
<?php
 $path = 'folder/';
print count(glob($path . '*.*'))
?>

simple as that :)
0
 
LVL 17

Accepted Solution

by:
nanharbison earned 500 total points
Comment Utility
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
<?PHP
 

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

0
 

Author Comment

by:nkrulez
Comment Utility
it works!! thanks alot
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now