ethan89 Posted September 9, 2010 Share Posted September 9, 2010 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; Quote Link to comment Share on other sites More sharing options...
pornophobic Posted September 9, 2010 Share Posted September 9, 2010 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. ?> Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 9, 2010 Author Share Posted September 9, 2010 At first I thought you code worked, but it displays 1 every time. Quote Link to comment Share on other sites More sharing options...
pornophobic Posted September 9, 2010 Share Posted September 9, 2010 I know it's something along those lines. I will google a little bit and let you know when I find something that works. Quote Link to comment Share on other sites More sharing options...
pornophobic Posted September 9, 2010 Share Posted September 9, 2010 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'; ?> Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 cool thx for sharing : ) Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 9, 2010 Author Share Posted September 9, 2010 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 Quote Link to comment 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.