我有php代码列出文件夹中的所有“.swf”文件。 (文件的名称始终是: “99-dd-mm-YY_HH-mm-ss.swf”,例如:“01-19-06-2011_18-40-00.swf”。 当我在文件夹中有超过500个文件时,很难看到并刷新页面。
我需要对文件列表进行分页。
<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 "\n<br>\n";
echo "<a href='javascript:this.location.reload();' style='color: #000000; font-weight: normal'>Refresh</a></br>";
echo "<tr>\n<td>\n<a href='javascript:javascript:history.go(-1)'>\n";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>\n";
echo "<b> Back</b></a></td>\n";
echo "\n</tr>\n";
echo "\n<br>\n\n<br>\n";
$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\n ";
echo "<input type='submit' name='xxx' value='$archiv'></td>\n";
echo "\n</tr>\n";
echo "<br>";
}
closedir($folder);
echo "\n<br>\n";
echo "<tr>\n<td>\n<a href='javascript:javascript:history.go(-1)'>\n";
echo "<img src='../../inc/img/back.png' alt='Back'";
echo " border=0>\n";
echo "<b> Back</b></a></td>\n";
echo "\n</tr>\n";
?>
</form>
</body>
</html>
答案 0 :(得分:1)
当需要浏览大量文件夹和文件时,请尝试Iterator对象。一个很好的例子:
function get_files($dir)
{
$dir = new DirectoryIterator($dir);
$list = iterator_to_array($dir, false);
return array_slice($list, 2);
}
这将获得所有文件名(如果您有php 5.3或更高版本)非常快,并将为您执行if dir_exists / file_exists! array_slice所以它删除了。和..目录。
答案 1 :(得分:0)
我使用此代码进行简单分页
<?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;
}
}
?>
这里有分页类和下载示例: http://lotsofcode.com/php/php-array-pagination.htm
谢谢大家!
答案 2 :(得分:0)
Like @Tessmore said,Spl Iterators teh awesomesauce 。根据{{3}},您只需要PHP&gt; 5.1用于基本迭代器。
the docs -
Cross-posting an example和DirectoryIterator
是我最好的朋友,虽然glob
似乎更容易预先过滤。您还可以编写自定义LimitIterator
。需要PHP&gt; 5.1,我想。
没有前置过滤器:
$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);
那么,做你的事:
foreach ($paginated as $file) { ... }
请注意,对于DirectoryIterator
示例,$file
将是FilterIterator
的实例,而glob
示例只是磁盘路径。