Php Count All Files In Directory

Active2 months ago

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.

The following code will list all the file in a directory

How to count number of files in folder in php? Ask Question. Up vote 1 down vote favorite. If you just want a count of the local files in a directory, you can use. (PHP 5 >= 5.3.0, PHP 7) GlobIterator::count — Get the number of directories and files. For example this won't works if no files are found in the target directory. PHP- List All Files In A Directory. PHP Scripts, Tips & Tricks. Here’s how to list all files in a directory. Would I need to count the images first and then.

While this is very simple code it does the job.

I'm now looking for a way to list ONLY files that have .xml (or .XML) at the end, how do I do that?

totallyNotLizards
5,9957 gold badges44 silver badges82 bronze badges
JessicaJessica
9174 gold badges21 silver badges37 bronze badges

9 Answers

A simple way to look at the extension using substr and strrpos

Bob FincheimerBob Fincheimer
15.2k1 gold badge22 silver badges52 bronze badges
NullUserException
68.2k23 gold badges184 silver badges220 bronze badges
David YellDavid Yell
10.1k10 gold badges57 silver badges95 bronze badges

You can also use the recursive variants of the iterators to traverse an entire directory hierarchy.

ArtefactoArtefacto
83.2k12 gold badges173 silver badges205 bronze badges
ZbigniewZbigniew

LIST FILES and FOLDERS in a directory (Full Code):
p.s. you have to uncomment the 5th line if you want only for specific extensions

T.ToduaT.Todua
34.3k12 gold badges151 silver badges145 bronze badges

You should use glob.

More about using glob and advanced filtering:

Otto-Ville LamminpääOtto-Ville Lamminpää

Simplest answer is to put another condition '.xml' strtolower(substr($file, -3)).

But I'd recommend using glob instead too.

instanceof me

Php Count All Files In Directory Python

instanceof me
30.5k3 gold badges24 silver badges36 bronze badges

You can extend the RecursiveFilterIterator class like this:

Now you can instantiate RecursiveDirectoryIterator with path as an argument like this:

This will list files under the current folder only.
To get the files in subdirectories also,pass the $iterator ( ExtensionFIlter Iterator) to RecursiveIteratorIterator as argument:

Now run the foreach loop on this iterator. You will get the files with specified extension

Note:-- Also make sure to run the ExtensionFilter before RecursiveIteratorIterator, otherwise you will get all the files

draGondraGon

Php Count Image Files In Directory

The $files array will get all files in the directory which the specified extension

Fasil PalanthodiFasil Palanthodi

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

Active2 years, 8 months ago

Is there a better/simpler way to find the number of images in a directory and output them to a variable?

Also Anyone can download Mera dil tujh pe kurban muraliya wale re full's newest and oldest mp3,hd mp4 songs. We provides Mera dil tujh pe kurban muraliya wale re full's songs in formats like mp4, hd, webm, mkv, flv, wmv, 3gp, wav, mp3. You can download free Mera dil tujh pe kurban muraliya wale re full's latest videos in High Definition FULL HD quality. Tujhpe dil kurbaan serial wikingii.

This seems like such a long way of doing this, is there no simpler way?

Note: The isImage() function returns true if the file is an image.

PHLAKPHLAK
9,47018 gold badges43 silver badges51 bronze badges

10 Answers

Check out the Standard PHP Library (aka SPL) for DirectoryIterator:

(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you'd need to filter out unseen stuff like . and . anyway.)

bbxbbybbxbby

This will give you the count of what is in your dir. I'll leave the part about counting only images to you as I am about to fallll aaasssllleeelppppppzzzzzzzzzzzzz.

rg88rg88
15.3k16 gold badges64 silver badges101 bronze badges
haheute

Php Get Directory

haheute
8842 gold badges17 silver badges45 bronze badges

The aforementioned code

is your best best, but the {jpg,png,gif} bit will only work if you append the GLOB_BRACE flag on the end:

PeeHaa
51.6k44 gold badges172 silver badges247 bronze badges
Josh DunbarJosh Dunbar

you could use glob..

or, I'm not sure how well this would suit your needs, but you could do this:

nickfnickf
389k176 gold badges595 silver badges692 bronze badges

You could also make use of the SPL to filter the contents of a DirectoryIterator using your isImage function by extending the abstract FilterIterator class.

You could then use iterator_count (or implement the Countable interface and use the native count function) to determine the number of images. For example:

Using this approach, depending on how you need to use this code, it might make more sense to move the isImage function into the ImageIterator class to have everything neatly wrapped up in one place.

salathesalathe
45k10 gold badges92 silver badges121 bronze badges

I use the following to get the count for all types of files in one directory in Laravel

Shailesh LadumorShailesh Ladumor

Your answer seems about as simple as you can get it. I can't think of a shorter way to it in either PHP or Perl.

Php Count All Files In Directory

You might be able to a system / exec command involving ls, wc, and grep if you are using Linux depending how complex isImage() is.

Regardless, I think what you have is quite sufficient. You only have to write the function once.

Chris KloberdanzChris Kloberdanz
3,0514 gold badges26 silver badges30 bronze badges

I use this to return a count of ALL files in a directory except . and .

Here is a good list of glob filters for file matching purposes.

Java Get All Files In Directory

MarcMarc
Matt
64.6k18 gold badges123 silver badges166 bronze badges
user2444847user2444847

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