Jump to content

How do I show the number of files in a folder/directory?


ethan89

Recommended Posts

I've tried this code and a few others, but it only displays 0.  I'd like the simplest working way to display the number of files in a directory.  Thanks for the help!

 

$directory = "../images/team/harry/";

$filecount = count(glob("" . $directory . "*.*"));

echo $filecount;

Link to comment
Share on other sites

Not sure if it's the proper way to do this, but here goes:

 

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        $thefiles = array();
        if ($file != "." && $file != "..") {
            array_push($thefiles, $file);
        }
    }
    closedir($handle);
}
$filecount = count($thefiles); //echo $filecount should give you the number of files in your directory. 
?>

 

Link to comment
Share on other sites

Not exactly, or anywhere near what I originally put, but this worked for me and it will give you the number of directories in your directory as well, if you wish. If not just delete that part out, I suppose.

 

<?php
$path = '/path/to/dir';
$numfile = 0;
$numdir = 0;
if ($handle = opendir($path)) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			$fName = $file;
			$file = $path.'/'.$file;
			if(is_file($file)) $numfile++;
			if(is_dir($file)) $numdir++;
			};
		};
		closedir($handle);
	};
echo $numfile.' files in a directory';
echo $numdir.' subdirectory in directory';
?> 

Link to comment
Share on other sites

Thanks!  It works perfectly!  I removed the number of directories parts of the code and made it shorter.  Here is my final code:

 

<?php

$path = '.';

$no = "0";

if ($handle = opendir($path))

{

while (false !== ($img = readdir($handle)))

{

if ($img != "." && $img != ".." && $img != "index.php")

{

$img = $path.'/'.$img;

if (is_file($img)) $no++;

}

}

closedir($handle);

}

echo "$no";

?>

 

SOLVED, Thanks phobic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.