Since the warning specifies the invalid argument is located in the foreach, you can ignore the do...while loop and the $nextrow and $lines variables.
It has to do with what $array contains. It must not actually be an array, so it's likely either NULL or FALSE, from what I've just read in the docs.
Try doing checks on the array variable, testing if it is empty, false, or null before you run the foreach loop which assumes that it is an array.
Also, the docs seem to use a while() loop with fgetscsv(). You might want to look into that.
nbst