2011-11-20 110 views
-1

我有用於列出文件夾中的所有「.swf」文件的php代碼。 (文件的名稱始終爲: 「99-dd-mm-YY_HH-mm-ss.swf」,例如:「01-19-06-2011_18-40-00.swf」。 當我有超過文件夾中的500個文件是複雜的,看和刷新頁面。在php中分頁列表的文件

我需要分頁文件的列表。

<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> 
+1

你需要分頁的文件列表和..東西不起作用?究竟是什麼問題。它會給出錯誤嗎? – Tessmore

回答

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

感謝所有!

1

當需要翻閱大量的文件夾和文件,請嘗試使用Iterator對象。一個很好的例子:

function get_files($dir) 
{ 
    $dir = new DirectoryIterator($dir); 
    $list = iterator_to_array($dir, false); 
    return array_slice($list, 2); 
} 

這將得到所有的文件名(如果你有php 5.3或更高版本r)非常快,並會爲你做dir_exists/file_exists! array_slice因此刪除了。和..目錄。

+0

感謝Tessmore,這給了我一個關於我的代碼的想法。 – MasterSieben

+0

還有['iterator_to_array()'](http://php.net/iterator_to_array)。 – hakre

+0

非常好!不知道那個功能:-) – Tessmore

0

Like @Tessmore said,spl迭代器是teh awesomesauce。根據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示例只是磁盤路徑。