Break if no file found in directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Break if no file found in directory

Post 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 !
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Break if no file found in directory

Post by califdon »

What do you mean by "break"? Specifically, what do you want to happen?
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: Break if no file found in directory

Post by Peuplarchie »

I simply want it to echo that there is no file.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Break if no file found in directory

Post 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.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: Break if no file found in directory

Post by Peuplarchie »

Code: Select all

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