Php Count All Files In Directory

Posted By admin On 15.01.20
  1. Php Count Files In Directory Recursive
  2. Php Count All Files In Directory
  3. List All Files In Directory Php
  4. List Files In Directory
  5. Php Get Directory

I’ve seen plenty of PHP code out there that does a file count in a directory. To be honest, most of the code i’ve seen has been bloated- code that is far too long. All you really need is 1 line of code to do a file count.

Active6 months ago
  • Join Stack Overflow to learn, share knowledge, and build your career.
  • PHP has functions that can be used for working with files and folders inside a directory. We are using 4 main functions in php opendir(), is_dir(),count() and readdir().
  • Executing glob('a?php') on the same list of files will only return aa.php and ab.php because as mentioned, the? Is the equivalent of pcre's dot, and is NOT the same as pcre's?, which would match 0 or 1 of the previous character.
  • Learn how to count the number of files in a directory/folder. You can even find the number in all sub-directories!

I'm working on a slightly new project. I wanted to know how to make it so it counts how many files are in a certain directory.

This is what I have so far (from searching). However it is not appearing properly? I have added a few notes so feel free to remove them, they are just so I can understand it as best as I can.

If you require some more information or feel as if I haven't described this enough please feel free to state so.

Ruddy

Php Count Files In Directory Recursive

8,0235 gold badges35 silver badges64 bronze badges
Bradly SpicerBradly Spicer
8032 gold badges15 silver badges31 bronze badges

14 Answers

BabaBaba
81.9k21 gold badges143 silver badges200 bronze badges

You can get the filecount like so:

where the '*' is you can change that to a specific filetype if you want like '*.jpg' or you could do multiple filetypes like this:

the GLOB_BRACE flag expands {a,b,c} to match 'a', 'b', or 'c'

JKirchartzGetJKirchartz
12.8k6 gold badges48 silver badges76 bronze badges
Laurent BrieuLaurent Brieu
2,4881 gold badge9 silver badges13 bronze badges
intelisintelis
3,4259 gold badges33 silver badges78 bronze badges
Nirav RanparaNirav Ranpara
13.9k3 gold badges36 silver badges56 bronze badges

Since I needed this too, I was curious as to which alternative was the fastest.

I found that -- if all you want is a file count -- Baba's solution is a lot faster than the others. I was quite surprised.

Try it out for yourself:

Test run: (obviously, glob() doesn't count dot-files)

vbwxvbwx

The best answer in my opinion:

  • It doesnt counts . and .
  • Its a one liner
  • Im proud of it
user10736793
Philipp WerminghausenPhilipp Werminghausen

array_slice works similary like substr function, only it works with arrays.

For example, this would chop out first two array keys from array:

Php Count All Files In Directory

And if You ommit the first parameter, like in first example, array will not contain first two key/value pairs *('.' and '.').

SpookySpooky

Maybe usefull to someone. On a Windows system, you can let Windows do the job by calling the dir-command. I use an absolute path, like E:/mydir/mysubdir.

List All Files In Directory Php

MichelMichel
2,4803 gold badges27 silver badges38 bronze badges

This should workenter the directory in dirname. and let the magic happen.

oussamaoussama

List Files In Directory

user3121984user3121984

Based on the accepted answer, here is a way to count all files in a directory RECURSIVELY:

Hp compaq dc7900 small form factor drivers windows 8. This page contains the list of device drivers for HP Compaq dc7900 Small Form Factor. To download the proper driver, first choose your operating system, then find. Find support and troubleshooting info including software, drivers, and manuals for your HP Compaq dc7900 Small Form Factor PC. This page contains the list of device drivers for HP Compaq dc7900. To download the proper driver, first choose your operating system, then find your device name and click the download button. Windows 10, Windows 8.1, Windows 7, Windows Vista, Windows XP: Download. Graphics Drivers. HP Pro 2110 Small Form Factor PC; HP. Download the latest driver, firmware, and software for your HP Compaq dc7900 Small Form Factor PC.This is HP's official website to download drivers free of cost for your HP Computing and Printing products for Windows and Mac operating system. Download HP Compaq dc7900 Small Form Factor PC Drivers for Windows 7, 8.1, 10, Just update HP Compaq dc7900 Small Form Factor PC drivers for your device now!

PanniPanni
parajs dfsbparajs dfsb

Php Get Directory

Not the answer you're looking for? Browse other questions tagged phpfilecountdirectory or ask your own question.