thyscorpion Posted October 19, 2006 Share Posted October 19, 2006 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. :'( Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/ Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 [code]<?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);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111069 Share on other sites More sharing options...
thyscorpion Posted October 19, 2006 Author Share Posted October 19, 2006 [quote author=Daniel0 link=topic=111982.msg454177#msg454177 date=1161239240][code]<?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);?>[/code][/quote]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') Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111075 Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 Sorry, fixed code: [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);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111081 Share on other sites More sharing options...
thyscorpion Posted October 19, 2006 Author Share Posted October 19, 2006 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.. ... ) Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111086 Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 Odd. It is correct syntax. Try this then: [code]<?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);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111103 Share on other sites More sharing options...
thyscorpion Posted October 19, 2006 Author Share Posted October 19, 2006 [quote author=Daniel0 link=topic=111982.msg454189#msg454189 date=1161240865]Sorry, fixed code: [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);?>[/code][/quote]hey got the problem!.. $var[1]=='jpg' is all it needs to check for a jpg.. :-).thanks a ton. ciao!.. :-) Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111164 Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 Nope. Imagine a file called.something.something.jpgUsing 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: [code]<?phpforeach(glob("*.txt") as $filename){ echo "{$filename}\n";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24409-how-to-count-number-of-jpg-files-in-a-folder/#findComment-111166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.