noob - number of files in a directory and making an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrBojangles
Forum Newbie
Posts: 2
Joined: Wed Oct 22, 2003 3:35 am
Location: Jail

noob - number of files in a directory and making an array

Post by MrBojangles »

Hi

I've been struggling with this and thought i'd turn to you for help

I've got a folder full of images numbered image1, image2 etc and a php file in the same folder that's supposed to count the number of files and return a variable - here's what i've tried but doesn't work

Code: Select all

$handle=opendir('.'); 
	$strSought = "jpg"; 
	$numImages=0;  
	while ($pic = readdir($handle)) 
	{ 
		if ($strSought == substr($pic, -3)) 
		{ 
		    $numImages = $numImages + 1; 
		} 
	} 
	closedir($handle);
what should i do to make it function ?
and also, at the moment i get the images to change by incrementing the number after the string "image" in the filename and then refreshing the page.
However, if i remove an image from the folder the sequence is broken and doesn't work ...
so, what method do i need to use to make an array from the names of all the files in the folder? I guess it's quite simple but my attempts have all failed ....

any help you can give will be received with gratitude

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

take a look at viewtopic.php?t=13806
MrBojangles
Forum Newbie
Posts: 2
Joined: Wed Oct 22, 2003 3:35 am
Location: Jail

Post by MrBojangles »

ok - i'll have a look

thanks
Post Reply