TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Break if no file found in directory (http://www.talkphp.com/advanced-php-programming/3618-break-if-no-file-found-directory.html)

Peuplarchie 11-17-2008 01:17 AM

Break if no file found in directory
 
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 :
PHP Code:

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 !

Wildhoney 11-17-2008 02:22 AM

How about something like this? Would this do the job for you?

php Code:
function get_files_by_extension($szDirectory, array $aExtensions)
{
    $szExtensions = vsprintf('*.%s', implode(',*.', $aExtensions));
    $szGlob = sprintf('%s/{%s}', rtrim($szDirectory, '/'), $szExtensions);
    return glob($szGlob, GLOB_BRACE | GLOB_NOSORT);
}

print_r(get_files_by_extension('myDirectory', array('php', 'ico', 'gif')));

Peuplarchie 11-17-2008 02:31 AM

Thanks for that pice of code...but it is too much for what I needed...
here's:
PHP Code:

if (empty($files)) 

    echo 
'No results'
    return 
null

return 

</span></span>


All times are GMT. The time now is 06:50 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0