The topic of 'pagination' has come up quite often on the forums; have you tried searching first? Note that the fact that you're trying to paginate results of a directory scan rather than rows from a DB query is irrelevant; both can be considered as a large pile of data of which you want to display one "page" at a time.
Also, unrelated to your issue, note that this:
if (preg_match('/.jpg/i', $file) || preg_match('/.gif/i', $file) || preg_match('/.png/i', $file)) {
is very, very wrong for a few reasons:
Regular expressions aren't even being used, so using a preg_*() function is wasteful, misleading, and possibly prone to errors (which you've; see #2).
Note that the period is used as a wildcard, so this pattern:
/.jpg/i
will successfully match this string:
MyVirus_IsNotAJpg.exe
There's no need to do any string manipulation to find a file's extension; that's one of the uses of the built-in [man]pathinfo/man function.