ted_chou12 Posted December 18, 2006 Share Posted December 18, 2006 I want to count the number of txt extension files in a directory, this should be pretty straight forward with glob() but my host doesnt allow me to use that function, so im getting a bit stuck.can anyone help me with this?thanks a lot!Ted. Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/ Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 put them into an array then count the array[code]<?php$files = array();if ($handle = opendir('pdffiles/')) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(strrchr($file, ".") == ".txt"){ $files[] = $file; } } }closedir($handle);}echo count($files);?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143875 Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 Ive made this into a function, but why doesnt it work, it always appears 0, but it works fine without the funcitonfunction threadcount($dir){$files = array();if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(strrchr($file, ".") == ".txt"){ $files[] = $file; } } }closedir($handle);}echo count($files);} Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143907 Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 sorry, my mistake... ;D accidentally insert the wrong directory name... Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143908 Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 should just return the value rather than echo it out in the function. works both ways i guess just preference[code]<?phpfunction threadcount($dir){$files = array();if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(strrchr($file, ".") == ".txt"){ $files[] = $file; } } }closedir($handle);}return count($files);}echo threadcount($folder);?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143912 Share on other sites More sharing options...
kenrbnsn Posted December 18, 2006 Share Posted December 18, 2006 When you make it a function, the function has to return something back to the calling procedure so you can use it. In your case you want to return the array:[code]<?phpfunction threadcount($dir){ $files = array(); if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != "..") if(strrchr($file, ".") == ".txt") $files[] = $file; } closedir($handle); } return ($files);}echo threadcount('.');?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143913 Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 one question about function, do you always have to place it before you use it? ??? Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143916 Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 yes you have to have the function established in order tou use it. What alot of developers do is create a file with all their functions in it. maybe call it function.inc.php. Now at the top of your page you require it so that the page stays cleaner.[code]require('functions.inc.php');[/code]Now all the functions you have written will be available to use. Ray Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143932 Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 [quote]do you always have to place it before you use it?[/quote]No. You could have all your functions at the bottom of the page if you wanted, php doesn't care as long as the function is within scope (which is what I think craygo is talking about). Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143937 Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 Yes thanks for clarifying that Thorpe. :) Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143943 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 Thorpe got there first, but I was just going to say that this:[code]<?php$number = 1;function doubleup($num){ return $num*2;}echo doubleup($number);?>[/code]Produces the same result as this:[code]<?php$number = 1;echo doubleup($number);function doubleup($num){ return $num*2;}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143946 Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 ok, i fully understood now. :D thanks to all of you.yeah. especially huggiebear ;D Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143947 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 No problem, I always try to provide an example in order to help understand.Huggie Quote Link to comment https://forums.phpfreaks.com/topic/31147-counting-the-number-of-txt-extension-files-in-a-directory/#findComment-143948 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.