I want to display files in my directory in browser. I know that this is possible using @opendir
and readdir
.. But what I want is to limit the number of files in the list to a specific number and display next using pagination.
-
Please vote if you find the question useful .. :) – Alfred Feb 24 '11 at 15:12
6 Answers
You could use scandir to read all the contents of the directory into an array. Then output the contents of the array based on the pagination value.
$offset = 10; //get this as input from the user, probably as a GET from a link
$quantity = 10; //number of items to display
$filelist = scandir('/mydir');
//get subset of file array
$selectedFiles = array_slice($filelist, $offset-1, $quantity);
//output appropriate items
foreach($selectedFiles as $file)
{
echo '<div class="file">'.$file.'</div>';
}

- 21,058
- 61
- 167
- 249

- 26,055
- 3
- 46
- 61
-
-
-
https://magento.stackexchange.com/questions/316910/magento-2-custom-added-ajax-pagination-not-working – Kirti Nariya Jul 10 '20 at 04:16
Cross-posting an example (also in this question) --
DirectoryIterator
and LimitIterator
are my new best friends, although glob
seems to prefilter more easily. You could also write a custom FilterIterator
. Needs PHP > 5.1, I think.
No prefilter:
$dir_iterator = new DirectoryIterator($dir);
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);
Glob prefilter:
$dir_glob = $dir . '/*.{jpg,gif,png}';
$dir_iterator = new ArrayObject(glob($dir_glob, GLOB_BRACE));
$dir_iterator = $dir_iterator->getIterator();
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);
Then, do your thing:
foreach ($paginated as $file) { ... }
Note that in the case of the DirectoryIterator
example, $file
will be an instance of SplFileInfo
, whereas glob
example is just the disk path.
-
When trying this code my $paginated is empty. Running a print_r on $dir_iteraror returns.... ArrayIterator Object ([storage:ArrayIterator:private] => ArrayObject Object([storage:ArrayObject:private] => Array (EXPECTED LIST OF ITEMS HERE). var_dump on paginated sends back object(LimitIterator)#3 (0) { } and a print_r gives LimitIterator Object(). My $page and $perpage vars are in bounds. Not sure why LimitIterator would return nothing if the $dir_iterator is full of files. Using php 5.3 – Mark Sep 06 '14 at 09:50
-
https://magento.stackexchange.com/questions/316910/magento-2-custom-added-ajax-pagination-not-working – Kirti Nariya Jul 10 '20 at 04:16
Depends on how you want to go about it. There are a ton of javascript/jquery pagination libraries out there .. just google "Javascript pagination."
If javascript is not an option or if you would prefer to just use php, then it should be relatively simple.
Use opendir/readdir to get a list of all the files. Set up however many you want for display. Divide the remainder by this number to get the number of pages. Then take a slice from the array of the (page - 1) * (number to list) up to (number to list). These are the files you will show. Pass the page number through get/post. If it's too high, then use the last page, too low or non-numeric, use the first page.

- 188,624
- 52
- 326
- 405
-
1
-
https://magento.stackexchange.com/questions/316910/magento-2-custom-added-ajax-pagination-not-working – Kirti Nariya Jul 10 '20 at 04:16
For pagination, you can use Zend_Paginator.
Once you get list of files in the directory, you only configure the paginator and it will take care of the rest.

- 5,047
- 2
- 26
- 32
Maybe something like this?
$page = 1;
$resultsPerPage = 10;
$files = array();
while(($obj = readdir($dir))) {
$files[] = $obj;
}
$limit = $page * $resultsPerPage;
($limit > count($files)) ? $limit = count($files) : $limit = $limit;
for($i = ($limit - $resultsPerPage); $i < $limit; $i++) {
echo($files[$i];
}
And then have your nav buttons modify the page number.

- 1,510
- 1
- 11
- 12
you should try YUI 2 pagination and maybe if you want to display the file in a table use the datatable there are a very usefull components from yahoo user interface
greetings

- 421
- 1
- 4
- 13