How can i count files in a folder but only the files starting with 15_ before the file name?

Recommended Answers

All 3 Replies

Member Avatar for diafol

have your looked at the directory and file functions in the php manual?

Here's the code.

<?php
$dir    = 'C:\';
$files = scandir($dir);
$j=0;
for ($i=0;$i<count($files);$i++){
	if (substr($files[$i],0,3)=="15_"){
		$j++;
	}
}
echo $j;
?>

This pulls all the files in the specified directory to an array and checks the array to see the stings starting with the required character i.e., "15_" and counts them.

Member Avatar for diafol

@sudeepjd
Please encourage the OP to have a go himself/herself. Just posting a solution won't help them in the long run.

I would say though, perhaps I'm old fashioned, but numbers at the start of file names?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.