2011-11-20 2 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; 
} 
} 
?> 

여기에 간단한 페이지 매기기에이 코드를 사용!

1

, 반복자 객체를보십시오. 좋은 예 :

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

이것은 모든 파일 이름을 가져옵니다 (PHP 5.3 또는 highe가있는 경우). r) 매우 빠르며 dir_exists/file_exists를 수행합니다. array_slice 그래서 그것을 제거합니다. 및 .. 디렉토리. 모든 http://lotsofcode.com/php/php-array-pagination.htm

감사 :

+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-glob보다 쉽게 ​​프리 필터 보인다 있지만

DirectoryIteratorLimitIterator은 나의 새로운 가장 친한 친구입니다. 맞춤 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 인 경우 $fileSplFileInfo의 인스턴스가되는 반면, glob 예는 디스크 경로 일뿐입니다.