Search code examples
phpfilefilesystemspaginationglob

PHP Filesystem Pagination


How does one paginate a large listing of files within a folder?

I can't see any functions in the PHP documentation that mention any way to specify an 'offset'. Both glob() and scandir() simply return all the files in the folder, and I'm afraid that won't be a good idea for a huge directory.

Is there any better way of doing this than simply going through all the files and chopping off the first X number of files? Note that I would like to have options for recursive traversal and using a glob() pattern.

Edit: I've looked a lot at LimitIterator, GlobIterator and RecursiveDirectoryIterator. They all seem nice but I have no idea where to even start if I was to combine them (The PHP SPL documentation is extremely sparse). I'm probably just over-thinking the problem.


Solution

  • You can paginate the results yourself, PHP returns a simple array, so you can keep a $offset and $limit variables so you know in which part of your array you are.

    If you are in a web context, you can pass these around in GET parameters.