QuadVods Posted May 21, 2006 Share Posted May 21, 2006 Hi,I'm using this script to count the number of files in a directory (e.g. I want to display: '10 Files hosted')The problem is that this code is returning '0' as the count, which is wrong.The index page in the root will have the counter on (www.domain.com)the directory with the files in is www.domain.com/1[code]//get path of directory$dir = ("/1/"); //open a handle to the directory$handle = opendir($dir);//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if (is_file($file) && $file !== '.' && $file !== '..') { $count++; }}echo $count; [/code]Any help would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/ Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 Is that a valid directory path. Put checks. Example:[code]//get path of directory$dir = '/1/'; // or maybe you meant './1/' or '/home/usr/www/1/'if (!is_dir($dir)) { echo $dir, ' is not a directory or is not found.'; exit;}//open a handle to the directory$handle = opendir($dir);if (!$handle) { echo $dir, ' could not be opened.'; exit;}//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { $count++; }}echo $count;[/code]EDIT:Try using:$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/'; Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37723 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 If I put that in I still get '0' Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37724 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 [!--quoteo(post=375808:date=May 21 2006, 10:24 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 10:24 AM) [snapback]375808[/snapback][/div][div class=\'quotemain\'][!--quotec--]If I put that in I still get '0'Take a look: www.digitaluploader.com The number should be along the bottom[/quote]Put what exactly?I edited my previous post. Use $_SERVER.While testing/debugging this, make sure you have error_reporting(E_ALL); at the top of your script so you can see all of PHP's errors/warnings/notices. Also, I assume you have display_errors on in the php.ini file. Otherwise, set it using ini_set('display_errors', '1'); at the top of your script too. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37725 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 Nope - I tried both the edits you mention, I just ge a '0'. I've got shared hosting, so I cant change any php settings.It is worth mentioning that the original script works and counts the files in the directory it is placed in...[code]//get path of directory containing this script$dir = $_SERVER['DOCUMENT_ROOT'].dirname($PHP_SELF); //open a handle to the directory$handle = opendir($dir);//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if (is_file($file) && $file !== '.' && $file !== '..') { $count++; }}echo $count;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37727 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 1) You can change some php.ini settings at run time.2) Don't use $PHP_SELF, unless you know register_globals is on. Use $_SERVER['PHP_SELF'] instead.3) It's just a matter of finding what your path is. Do a phpinfo and find out.FYI:I just managed to upload a Windows .exe file disguised as .jpg.You can delete the one I just uploaded:[a href=\"http://www.DigitalUploader.com/viewer.php?id=7449application.jpg\" target=\"_blank\"]http://www.DigitalUploader.com/viewer.php?...application.jpg[/a]See this post for how to prevent this sort of thing:[a href=\"http://www.phpfreaks.com/forums/index.php?s=&showtopic=93371&view=findpost&p=373601\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...ndpost&p=373601[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37731 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 editDoes that help?Thanks for the other info - i'll check that out Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37733 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 Well, this should work (as I posted before):// Becomes: /kunden/homepages/23/d163968312/htdocs/upload/1/$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';Don't use 'php_self' because you're script is probably not running in the '/1/' directory. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37735 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 [!--quoteo(post=375819:date=May 21 2006, 06:49 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 21 2006, 06:49 PM) [snapback]375819[/snapback][/div][div class=\'quotemain\'][!--quotec--]Well, this should work (as I posted before):$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/'; // /kunden/homepages/23/d163968312/htdocs/upload/1/Don't use 'php_self' because you're script is probably not running in the '/1/' directory.[/quote]It still just says 0...There are 8 files in the directory at this moment.And yes - the script isnt in the 1 directory. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37737 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 [!--quoteo(post=375821:date=May 21 2006, 10:52 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 10:52 AM) [snapback]375821[/snapback][/div][div class=\'quotemain\'][!--quotec--]It still just says 0...There are 8 files in the directory at this moment.And yes - the script isnt in the 1 directory.[/quote]The last code you posted didn't include other stuff I've recommended you do. So, display the $dir value, include the is_dir(), check the $handle variable, and put echo's in the while loop to help yourself debug this problem.Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37740 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 [!--quoteo(post=375824:date=May 21 2006, 06:59 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 21 2006, 06:59 PM) [snapback]375824[/snapback][/div][div class=\'quotemain\'][!--quotec--]The last code you posted didn't include other stuff I've recommended you do. So, display the $dir value, include the is_dir(), check the $handle variable, and put echo's in the while loop to help yourself debug this problem.Good luck.[/quote]The last code was the original script before I edited it.So this is what I have to far: [code]//get path of directory$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/'; // /kunden/homepages/23/d163968312/htdocs/upload/1/if (!is_dir($dir)) { echo $dir, ' is not a directory or is not found.'; exit;}//open a handle to the directory$handle = opendir($dir);if (!$handle) { echo $dir, ' could not be opened.'; exit;}//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { $count++; }}echo $count;[/code]It still just retuns the '0'. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37742 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 humor me. move $count++ outside the if statement like so:[code]//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { // $count++; } $count++;}echo $count;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37750 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 Well, I don't know what's going on. Put displays in the while loop to see if it's getting in there. There's also a command to clear out the directory buffer because PHP cache's that, but I can't remember it's name. If I do, I'll post it.You could try a different approach:[code]// Becomes: /kunden/homepages/23/d163968312/htdocs/upload/1/$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';$ary = glob($dir . '*.*'); // Needs 4.3.0+ which you haveecho 'File count: ', count($ary), '<br/>';[/code]EDIT:FYI: clearstatchache():[a href=\"http://us2.php.net/manual/en/function.clearstatcache.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.clearstatcache.php[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37751 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 [!--quoteo(post=375834:date=May 21 2006, 07:25 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 07:25 PM) [snapback]375834[/snapback][/div][div class=\'quotemain\'][!--quotec--]humor me. move $count++ outside the if statement like so:[code]//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { // $count++; } $count++;}echo $count;[/code][/quote] [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] It works, It works! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] Thanks very much both of you! Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37756 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 umm, well then shouldn't it now be returning the count, including . and .. ? Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37757 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 [!--quoteo(post=375841:date=May 21 2006, 07:35 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 07:35 PM) [snapback]375841[/snapback][/div][div class=\'quotemain\'][!--quotec--]umm, well then shouldn't it now be returning the count, including . and .. ?[/quote]Well the counter is 2 out of the actual figure.. but thats close enough for me! If you know why that is.. that would be super.Code I have now:[code]//get path of directory$dir = $_SERVER['DOCUMENT_ROOT'] . '/1/';//open a handle to the directory$handle = opendir($dir);if (!$handle) { echo $dir, ' could not be opened.'; exit;}//intitialize our counter$count = 0;//loop through the directorywhile (false !== ($file = readdir($handle))) { //evaluate each entry, removing the . & .. entries if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { // $count++; }$count++;}echo $count;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37758 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 [!--quoteo(post=375840:date=May 21 2006, 11:32 AM:name=QuadVods)--][div class=\'quotetop\']QUOTE(QuadVods @ May 21 2006, 11:32 AM) [snapback]375840[/snapback][/div][div class=\'quotemain\'][!--quotec--][img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] It works, It works! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]Thanks very much both of you![/quote]I wouldn't get excited just yet. Is the count right?I would change this:if ((is_file($file)) && ($file !== '.') && ($file !== '..')) { $count++;}to this:if ((is_file($file)) && ($file != '.') && ($file != '..')) { $count++;}Of course take the count outside the 'if' statment out. Then see what the count is.Otherwise, just use the glob() function as demonstrated in one of my earlier posts. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37759 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 i think you said earlier you had 8 files, right? so it's now returning 10, right? it's because it's counting the '.' and '..' as files. the problem is with that if statement. the easy ghetto solution to this is to simply subtract 2 from count. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37760 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 [!--quoteo(post=375844:date=May 21 2006, 11:40 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 11:40 AM) [snapback]375844[/snapback][/div][div class=\'quotemain\'][!--quotec--]i think you said earlier you had 8 files, right? so it's now returning 10, right? it's because it's counting the '.' and '..' as files. the problem is with that if statement. the easy ghetto solution to this is to simply subtract 2 from count.[/quote]Ah, cheating instead of understanding why it's doing what it's doing. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]Hey, do what you want. But the !== is more restrictive than just != since it compares the type too. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37762 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 @toplay:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]if ((is_file($file)) && ($file != '.') && ($file != '..')) {$count++;}[/quote]Still gives 2 more files than I have.I'm up for the ghetto method if you don't want to worry about it.. help with that or a true fix would be great. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37764 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 well i was going to eventually get to that. i actually started posting about it such and all but i did a preview and saw you beat me to it so i just ditched the post except for the cheat [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]after all is said and done, just do $count = $count - 2; Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37765 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 Fantastic! Thanks again everyone [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37766 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 Well, you could even cut the if statement to just this:if (is_file($file)) { $count++;}Please show us the filenames/directories of what you actually have in that '1' directory now.FYI:Short cut to subtracting is: $count -= 2; Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37767 Share on other sites More sharing options...
QuadVods Posted May 21, 2006 Author Share Posted May 21, 2006 . Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37770 Share on other sites More sharing options...
toplay Posted May 21, 2006 Share Posted May 21, 2006 We forgot to specify the full path to the file on the is_file() function. The readdir() doesn't return a pull path and the filename, but rather just the filename.if ((is_file($dir . $file)) && ($file != '.') && ($file != '..')) {$count++;}Please try that and get back to us. Quote Link to comment https://forums.phpfreaks.com/topic/10128-counting-files-in-a-directory/#findComment-37771 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.