Jump to content

[SOLVED] is_file not working


imperium2335

Recommended Posts

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

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!

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))

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.