Jump to content


Photo

Counting number of files in a folder?


  • Please log in to reply
4 replies to this topic

#1 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 09 August 2006 - 10:32 PM

I'm writing a script that allows user to upload their pics to a folder (each user has his/her own folder). That folder will contain only pics. Now, I want it so that I can count the number of files are in the folder.
Is there a function for this? Sort of like count() but for directory?

#2 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 09 August 2006 - 10:43 PM

$dir    = '/dir'; //folder to scan
$files = scandir($dir); //scans the folder
$count_of_files = count($files) - 2; // - 2 to account for "." and ".."

*edit*
This is for PHP5. What version are you running?
Make a donation to phpfreaks. They need your help![br][br]

#3 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 09 August 2006 - 11:39 PM

PHP 4

I'm only using PHP 4 since my client's webhost does not support PHP 5.

#4 kalivos

kalivos
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationCalifornia

Posted 10 August 2006 - 04:56 PM

PHP 4 version

$dir = "/dir";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  $files[] = $filename;
}

print_r($files);
Make a donation to phpfreaks. They need your help![br][br]

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 August 2006 - 05:06 PM

If you're running version 4.3 or higher, take a look at the glob() function http://www.php.net/glob

<?php
echo count(glob('/dir/*'));
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users