Page 1 of 1

Break if no file found in directory

Posted: Sun Nov 16, 2008 7:18 pm
by Peuplarchie
Good day to you,
I have a function which return an array of files.
I would need it to break if there is no file in the directory.

Here is the code :

Code: Select all

function compileList($extensions)
{
     if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          $ext = strtolower(end(explode('.', $file)));
       
          if (in_array($ext, $extensions) AND $file != "." AND $file != "..")
              {
                  $files[$file]=implode(file($file));
              }
       }
  closedir($handle);
  }
  return $files;
         
}
Thanks !

Re: Break if no file found in directory

Posted: Sun Nov 16, 2008 7:39 pm
by califdon
What do you mean by "break"? Specifically, what do you want to happen?

Re: Break if no file found in directory

Posted: Sun Nov 16, 2008 7:44 pm
by Peuplarchie
I simply want it to echo that there is no file.

Re: Break if no file found in directory

Posted: Sun Nov 16, 2008 8:31 pm
by califdon
As I understand your purpose, if there are no files of the type passed to the function as an argument, the variable $files will not be set. So you could probably just test for whether the function returns a value, and if not, echo your message. I would not recommend trying to send the message from within the function.

Re: Break if no file found in directory

Posted: Sun Nov 16, 2008 8:37 pm
by Peuplarchie

Code: Select all

 
if (empty($files))
{
    echo 'No results';
    return null;
}
return