imperium2335 Posted April 3, 2009 Share Posted April 3, 2009 Could someone please help me and tell me why this isnt working? I just get 0 but if i remove the is_dir bit i get 5 (2 folders and 3 files) I just want it to return the number of files, I've tried using is_file but i get the same result. $openzone = opendir($dir) ; while($file = readdir($openzone)) { if($file != '.' && $file != '..' && !is_dir($file)) { $filecount++ ; $w = rand(0,$arrayamt) ; //rename($file, $firstword . $wordbox[$w]) ; } } echo "$filecount" ; Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 The logic in this part is wrong. if($file != '.' && $file != '..' && !is_dir($file)) Although it's written as we'd think it should be, try this: if($file != '.' && $file != '..') { if (!is_dir($file)) { $filecount++ ; $w = rand(0,$arrayamt) ; //rename($file, $firstword . $wordbox[$w]) ; } } I had to use 2 if()'s - a bit unsure myself! Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800262 Share on other sites More sharing options...
imperium2335 Posted April 3, 2009 Author Share Posted April 3, 2009 Thanks, but it still doesn't work get same result Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800266 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 if (!is_dir($file)) { $filecount++ ; $w = rand(0,$arrayamt) ; //rename($file, $firstword . $wordbox[$w]) ; } You're skipping the folders anyway so why check for the . and ..? See if that works. Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800267 Share on other sites More sharing options...
imperium2335 Posted April 3, 2009 Author Share Posted April 3, 2009 thats to exclude the current and top level folders from being included in the count. Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800268 Share on other sites More sharing options...
Mark Baker Posted April 3, 2009 Share Posted April 3, 2009 is_file() doesn't know what directory you're checking against, only what the file name is, so it's defaulting to the current directory. if($file != '.' && $file != '..' && !is_dir($dir.'/'.$file)) Incidentally, both . and .. will return true for is_dir() (as Yesideez says), so the checks for those are redundant if(!is_dir($dir.'/'.$file)) Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800283 Share on other sites More sharing options...
imperium2335 Posted April 3, 2009 Author Share Posted April 3, 2009 Cheers! Link to comment https://forums.phpfreaks.com/topic/152379-solved-is_file-not-working/#findComment-800290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.