开发者

Limit number of results for glob directory/folder listing

开发者 https://www.devze.com 2023-02-20 07:31 出处:网络
How would I go about limiting the number of directory results/pdfs to, say 8, in the following code? $counter = 0;

How would I go about limiting the number of directory results/pdfs to, say 8, in the following code?

$counter = 0; 
foreach (glob("/directory/*.pdf") as $path) { //configure path
    $docs[filectime($path)] = $path;
} 
krsort($docs); // sort by key (timestamp)               
foreach ($docs as $timestamp => $path) {
    echo '<li><a href="/directory/'.basename($path).'" class="'.($counter%2?"light-grey":"").'" tar开发者_Go百科get="_blank">'.basename($path).'</a></li>';
    $counter++;                     
}

This is probably really easy but I can't seem to be able figure it out - thanks in advance, S.


foreach (array_slice(glob("/directory/*.pdf"),0,8) as $path) {


Do a check of the counter and when it hits a certain number then break; from the loop.


Limit number of results for glob directory/folder listing

Glob does not have a flag to limit the number of results. This means you would have to retrieving all results for that directory and then reduce the array down to 8 file paths.

$glob = glob("/directory/*.pdf");
$limit = 8;
$paths = array();

if($glob){ //$glob not false or empty 

    if(count($glob) > $limit)){ // if number of file paths is more than 8
        $paths = array_slice($glob,0,$limit);//get first 8 (alphabetically)
    } else {
        // we don't need to splice because there are less that 8 results)
        $paths = $glob;
    }

    // or ternary 
    $paths = (count($glob) > $limit) ? array_slice($glob,0,$limit) : $glob;
}

foreach ($paths as $path){
    ...
}

Taking a deeper look at you example this might not be what your actually looking for as you want to sort your results.

When possible you should make use out of the GLOB FLAGS. Although there is not a flag to return the files in a specific order you can stop glob from returning them in alphabetical order(default). If you ever need to sort the files in any way other than alphabetically then always use the GLOB_NOSORT flag.

If you did want the array limited to 8 filepaths but also have them in order of the timestamp then you would have to order them first before splicing the array.

$paths = array();

$limit = 8;
$glob = glob("/directory/*.pdf",GLOB_NOSORT); // get all files

if($glob){ // not false or empty

    // Sort files by  inode change time
    usort($glob, function($a, $b){
       return filectime($a) - filectime($b);
    });
    $paths = (count($glob) > $limit) ? array_slice($glob,0,$limit) : $glob;
}

$docs = array_merge($docs, $paths); // As i couldn't see where $docs was set I didn't want to overwrite the variable.

foreach ($docs as $path) {
    $basename = basename($path);
    echo '<li><a href="/directory/'.$basename.'" " target="_blank">'.$basename.'</a></li>';
}

<style> li > a:nth-child(even){ color:#fff; background-color:lightgrey; } </style>

0

精彩评论

暂无评论...
验证码 换一张
取 消