开发者

VIew files in directory with pagination - php

I want to display files in my d开发者_运维百科irectory 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.


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>'; 
}


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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜