2010-03-18 31 views
0

$ d = opendir(「docs」);

while (($file = readdir($d)) !== false) { 

    if (($file != ".") && ($file != "..")){ 
     } 
} 

我希望能夠顯示前5個文件名。

這裏是扭曲的,我有一個按鈕,我想顯示下一個5名

感謝 讓

回答

2

步驟你可以使用一個計數器變量,並確保它不會去更多的5?

這樣的有點:

$counter = 0; 
$d = opendir("docs"); 
while (($file = readdir($d)) !== false && $counter < 5) { 
    if (($file != ".") && ($file != "..")){ 
     // ... 
     $counter++; 
    } 
} 


未測試:也許你將不得不使用<=代替<

+0

問題已被編輯 計數器工作正常,但它不會讓所顯示的剩餘文件名,當下一個按鈕被按下 – X10nD 2010-03-18 07:46:39

0

您可以使用計數器如下:

$d = opendir("docs"); 
// check for opendir error here. 

$count = 0; // initialize counter. 

while (($file = readdir($d)) !== false) { 

    if (($file != ".") && ($file != "..")){ 
     //....process files here. 

     $count++; // done with one file..increment counter. 
     if($count == 5) // have we reached the limit ? 
      break; // if yes break. 


    } 
} 
+0

這看起來對我來說,它會只顯示4;) – Chris 2010-03-18 07:42:26

+0

問題已被編輯 – X10nD 2010-03-18 07:47:54

1

您可以先將所有文件全部放入數組中。然後使用一個循環來在陣列上,在5

$filenames = array_filter(glob($path.'*'), 'is_file'); 
+0

問題已被編輯 – X10nD 2010-03-18 07:47:17

-1
$from = GET["from"]; 

$counter = 0; 
$fileCounter = 0; 
$d = opendir("docs"); 
while (($file = readdir($d)) !== false && $counter < 5) { 
    if (($file != ".") && ($file != "..") && $from == $fileCounter){ 
     // ... 
     $counter++; 
    } else { 
     $fileCounter++; 
    } 
} 

上面的代碼是不是testet但它應該工作類似的東西。

在下一個按鈕上,您現在可以使用files.php?from = 5來獲取5-10中的文件。

0

@Edited問題

我建議的下一個按鈕提交一個表單或以某種方式創建一個新的要求? 所以只需傳遞當前的計數器值。

$counterLimit = 5; 
$counterStart = $_POST['counterStart']; //Pass the start value 
$counterEnd= ($counterLimit + $counterStart); 
$dir = opendir("dir"); 
$i = 0; 
while (($file = readdir($dir) !== false) && ($i <= $counterEnd)) { 
    if ($i >= $counterStart) { 
     //Do something 
    } 
    $i++; 
} 

未測試。但是這樣的事情。