Jump to content


Photo

How to count number of jpg files in a folder


  • Please log in to reply
7 replies to this topic

#1 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 19 October 2006 - 06:00 AM

Hi  :) i am a intermediate guy in PHP.. wanted to ask a simple thing..

I am making a personal site and am sick of manually giving links to each image in my gallery.. >:(

can i firstly, come to know the number of jpg files in a dir.?
secondly, can i pick up each's name one by one? (to use it in the href tag)
???

Please help. :'(



#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 06:27 AM

<?php
$dh = readdir("the_dir");
while($item = readdir($dh))
{
	$var = explode('.',$item);
	if(is_file($item) && $var[count($var)-1]=='jpg')
	{
		echo "{$item}\n";
	}
}
closedir($dh);
?>


#3 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 19 October 2006 - 06:43 AM

<?php
$dh = readdir("the_dir");
while($item = readdir($dh))
{
	$var = explode('.',$item);
	if(is_file($item) && $var[count($var)-1]=='jpg')
	{
		echo "{$item}\n";
	}
}
closedir($dh);
?>

Hi thanks.. when i use the code. it gives an error as follows:
------
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in
---------
it's refering to the line : if(is_file($item) && $var[count($var)-1]=='jpg')


#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 06:54 AM

Sorry, fixed code:
<?php
$dh = opendir("the_dir");
while($item = readdir($dh))
{
	$var = explode('.',$item);
	if(is_file($item) && $var[count($var)-1]=='jpg')
	{
		echo "{$item}\n";
	}
}
closedir($dh);
?>


#5 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 19 October 2006 - 07:07 AM

hey sorry to bother you again!.. it still didnt work. giving the same error. same line.

am using PHP 4 if that helps..

(thou i laughed seeing the small correction u made. i am also a programmer but of C++/java etc.. ... )



#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 08:08 AM

Odd. It is correct syntax. Try this then:
<?php
$dh = opendir("the_dir");
while($item = readdir($dh))
{
	if(is_file($item) && $var[count($var)-1]=='jpg')
	{
		$var = explode('.',$item);
		$count = count($var);
		$extension = $var[$count-1];
		if($extension == 'jpg')
			echo "{$item}\n";
		}
	}
}
closedir($dh);
?>


#7 thyscorpion

thyscorpion
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 19 October 2006 - 11:24 AM

Sorry, fixed code:

<?php
$dh = opendir("the_dir");
while($item = readdir($dh))
{
	$var = explode('.',$item);
	if(is_file($item) && $var[count($var)-1]=='jpg')
	{
		echo "{$item}\n";
	}
}
closedir($dh);
?>


hey got the problem!..
$var[1]=='jpg' is all it needs to check for a jpg.. :-).
thanks a ton.
ciao!.. :-)

#8 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 11:30 AM

Nope. Imagine a file called.

something.something.jpg
Using what you said would take something as the extension. There is a function to get the parts of a filename, but I can't remember it.

An alternative way of doing it would be:
<?php
foreach(glob("*.txt") as $filename)
{
	echo "{$filename}\n";
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users