I have php code for list all ".swf" files in a folder. (The name of the files is always: "99-dd-mm-YY_HH-mm-ss.swf", example: "01-19-06-2011_18-40-00.swf". When I have more than 500 files in the folder is complicated to see and to refresh the page.
I need paginate the list of files.
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title></title>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>
</head>
<body>
<form name="myform" action="some.php" method="post">
<?php
echo "
<br>
";
echo "<a href='javascript:this.location.reload();' style='color: #000000; font-weight: normal'>Refresh</a></br>";
echo "<tr>
<td>
<a href='javascript:javascript:history.go(-1)'>
";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>
";
echo "<b> Back</b></a></td>
";
echo "
</tr>
";
echo "
<br>
<br>
";
$folder='.';
function order($a,$b){
global $folder;
$directory='.';
return strcmp(strtolower($a), strtolower($b));
}
$folder=opendir($folder);
while($files=readdir($folder)){
$ext = pathinfo($files, PATHINFO_EXTENSION);
if ($ext == 'swf') { //con esta línea saco el nombre index.php del listado
$file[]=$files;
usort($file, "order");
}
}
$n = 0;
foreach($file as $archiv){
$n = $n + 1;
$day = substr($archiv, 3,10);
$day = str_replace("-","/", $day);
$hour = substr($archiv, 14,8);
$hour = str_replace("-",":", $hour);
echo "<img alt='Ver $archiv' src='../../inc/img/video.png'> Video $n, Día: $day, hour: $hour
";
echo "<input type='submit' name='xxx' value='$archiv'></td>
";
echo "
</tr>
";
echo "<br>";
}
closedir($folder);
echo "
<br>
";
echo "<tr>
<td>
<a href='javascript:javascript:history.go(-1)'>
";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>
";
echo "<b> Back</b></a></td>
";
echo "
</tr>
";
?>
</form>
</body>
</html>
I used this code for simple pagination
<?php
// Include the pagination class
include 'pagination.class.php';
// Create the pagination object
$pagination = new pagination;
// some example data
foreach (range(1, 100) as $value) {
$products[] = array(
'Product' => 'Product '.$value,
'Price' => rand(100, 1000),
);
}
// If we have an array with items
if (count($products)) {
// Parse through the pagination class
$productPages = $pagination->generate($products, 20);
// If we have items
if (count($productPages) != 0) {
// Create the page numbers
echo $pageNumbers = '<div>'.$pagination->links().'</div>';
// Loop through all the items in the array
foreach ($productPages as $productID => $productArray) {
// Show the information about the item
echo '<p><b>'.$productArray['Product'].'</b> 243'.$productArray['Price'].'</p>';
}
// print out the page numbers beneath the results
echo $pageNumbers;
}
}
?>
Here there is pagination class and the example for download: http://lotsofcode.com/php/php-array-pagination.htm
Thanks for all!
When required to go through lots of folders and files, try the Iterator object. A nice example:
function get_files($dir)
{
$dir = new DirectoryIterator($dir);
$list = iterator_to_array($dir, false);
return array_slice($list, 2);
}
This will get all the file names (if you have php 5.3 or higher) very fast and will do the if dir_exists / file_exists for you! The array_slice so it removes the . and .. directory.
Like @Tessmore said, Spl Iterators are teh awesomesauce. According to the docs, you only need PHP > 5.1 for the basic iterators.
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.