Peuplarchie Posted November 17, 2008 Share Posted November 17, 2008 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 : 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 ! Link to comment https://forums.phpfreaks.com/topic/133001-break-if-no-file-found-in-directory/ Share on other sites More sharing options...
Maq Posted November 17, 2008 Share Posted November 17, 2008 What's your question...? Link to comment https://forums.phpfreaks.com/topic/133001-break-if-no-file-found-in-directory/#findComment-691750 Share on other sites More sharing options...
Peuplarchie Posted November 17, 2008 Author Share Posted November 17, 2008 how if there is no file in the directory, instead of stating an error it would echo, no file in directory... Link to comment https://forums.phpfreaks.com/topic/133001-break-if-no-file-found-in-directory/#findComment-691752 Share on other sites More sharing options...
Peuplarchie Posted November 17, 2008 Author Share Posted November 17, 2008 if (empty($files)) { echo 'No results'; return null; } return Link to comment https://forums.phpfreaks.com/topic/133001-break-if-no-file-found-in-directory/#findComment-691785 Share on other sites More sharing options...
ddrudik Posted November 17, 2008 Share Posted November 17, 2008 <pre> <?php function compileList2($extensions){ $extlist=implode(array_merge(array_map('strtolower',$extensions),array_map('strtoupper',$extensions)),','); $ret=glob('*.{'.$extlist.'}',GLOB_BRACE); if(!isset($ret) || (isset($ret) && count($ret)==0)){ echo "No files found."; return NULL; } else { return $ret; } } echo print_r(compileList2(array('txt','php')),true); echo print_r(compileList2(array('does_not_exist')),true); ?> Link to comment https://forums.phpfreaks.com/topic/133001-break-if-no-file-found-in-directory/#findComment-692012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.