filename is supposed to be one file, not a list of files. So it should be enough to break, check for null filename later instead of count($list), and don't use a foreach at all. Am I oversimplifying?