Just try this--
if (glob($directory . "*.jpg") != false) $filecount = count(glob($directory . "*.jpg")); else $filecount = 0;