【问题标题】:Paginate list of files in php在php中对文件列表进行分页
【发布时间】:2012-01-02 12:18:56
【问题描述】:

我有列出文件夹中所有“.swf”文件的 php 代码。 (文件名始终为: “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>&nbsp;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>&nbsp;Back</b></a></td>\n";
   echo "\n</tr>\n";
?>
</form>
</body>
</html>

【问题讨论】:

  • 您需要对文件列表进行分页,然后.. 有什么不工作?究竟是什么问题。会报错吗?

标签: php file-io pagination


【解决方案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 所以它删除了 .和..目录。

【讨论】:

  • 非常好!不知道那个功能:-)
【解决方案2】:

我用这段代码进行简单的分页

<?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

谢谢大家!

【讨论】:

    【解决方案3】:

    Like @Tessmore said,Spl 迭代器是真棒酱。根据the docs,基本迭代器只需要 PHP > 5.1。

    Cross-posting an example --

    DirectoryIteratorLimitIterator 是我最好的新朋友,尽管glob 似乎更容易进行预过滤。你也可以写一个自定义的FilterIterator。我认为需要 PHP > 5.1。

    无前置过滤器:

    $dir_iterator = new DirectoryIterator($dir);
    $paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);
    

    全局预过滤器:

    $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 将是SplFileInfo 的一个实例,而glob 示例只是磁盘路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 2015-05-30
      • 2020-02-18
      • 2019-03-12
      • 2015-02-14
      • 1970-01-01
      • 2018-06-13
      • 2010-09-20
      相关资源
      最近更新 更多