Put a new table row every time an incremented number + 1 divided by the number of columns equals an integer. Also, the incremented number + 1 mod the number of columns should equal zero.
Assuming, when you loop through all the files (to print them out), that you increment a number, let's say $i, this number will keep getting larger and larger.
If you specified two columns and have 5 images, it will start at 0 for the first image, go to 1 for the second, then go to 3 for the next. If we add 1 to all of these numbers (just set it to 1 in the first place), then we now have the numbers 1,2,3,4,5. The numbers, when divided by two, will return with .5,1,1.5,2,2.5. 1 and 2 are both integers, so we can end the row here, since we know this is the last image in the row. We can either divide (/) here or use the mod (%) operator.
2%2 = 0, 2%2=1.
Here are some conditional statements you can use:
// Divide
if (is_int($i % $columns)) {
echo("</tr><tr>");
}
// Mod
if ($i % $columns) {
echo("</tr><tr>");
}