将文件名添加到glob脚本

时间:2014-04-15 19:02:44

标签: php glob

我使用以下脚本在页面上显示我的所有图像。我希望在这个文件夹中有大约12000张图像,因此需要分页。文件名是按升序1到12000的数字,必须按顺序显示。

<?php

$dir = "../image/thumb/";

//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg";

//Set the number of images you want to display per page
$imagesPerPage = 20;

//Set the $page variable
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}

//Load all images into an array
$images = glob($dir."*.{".$extensions."}", GLOB_BRACE);

//Count the number of images
$totalImages = count($images);

//Get the total pages
$totalPages = ceil($totalImages / $imagesPerPage);

//Make sure the page you are on is not greater then the total pages available.
if($page > $totalPages){
//Set the currnet page to the total pages.
$page = $totalPages;
}

//Now find where to start the loading from
$from = ($page * $imagesPerPage) - $imagesPerPage;

//Now start looping
for($i = $from; $i < ($from + $imagesPerPage); $i++){
//We need to make sure that its within the range of totalImages.
if($i < $totalImages){
    //Now we can display the image!
    echo "<img src='{$images[$i]}' alt='{$images[$i]}' />";
}
}

//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
if($p == $page){
    $tmp_pages[] = "<strong>{$p}</strong>";
}else{
    $tmp_pages[] = "<a href='?page={$p}'>{$p}</a>";
}
}

//Now display pages, seperated by a hyphon.
echo "<br />" . implode(" - ", $tmp_pages);
?>

我需要让每个图像下面的文件名回显,我该如何实现?

非常感谢提前。

=====编辑=====

此脚本现在可以使用SuperScript了。完整的代码是:

<?php

//The directory to your images folder, with trailing slash
$dir = "../image/thumb/";

//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg";

//Set the number of images you want to display per page
$imagesPerPage = 20;

//Set the $page variable
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}

//Load all images into an array
$images = glob($dir."*.{".$extensions."}", GLOB_BRACE); 

usort($images,function($a,$b){ 
$a=(int)preg_replace('~\D~','',$a); 
$b=(int)preg_replace('~\D~','',$b); 
return $a<$b?-1:1; 
});

//Count the number of images
$totalImages = count($images);

//Get the total pages
$totalPages = ceil($totalImages / $imagesPerPage);

//Make sure the page you are on is not greater then the total pages available.
if($page > $totalPages){
//Set the currnet page to the total pages.
$page = $totalPages;
}

//Now find where to start the loading from
$from = ($page * $imagesPerPage) - $imagesPerPage;

//Now start looping
for($i = $from; $i < ($from + $imagesPerPage); $i++){
   //We need to make sure that its within the range of totalImages.
    if($i < $totalImages){
    //Now we can display the image!
    // echo "<img src='{$images[$i]}' alt='{$images[$i]}' /><a class=\"filename\">" .     basename($images[i]) . "</a>";
echo "<img src='{$images[$i]}' alt='{$images[$i]}' /><p>" . basename($images[$i]) . "        </p>";
}
}

//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
if($p == $page){
    $tmp_pages[] = "<strong>{$p}</strong>";
}else{
    $tmp_pages[] = "<a href='?page={$p}'>{$p}</a>";
}
}

//Now display pages, seperated by a hyphon.
echo "<br />" . implode(" - ", $tmp_pages);
?>

1 个答案:

答案 0 :(得分:2)

尝试更改echo行(//Now we can display the image!):

echo "<img src='{$images[$i]}' alt='{$images[$i]}' /><p>" . basename($images[$i]) . "</p>";

basename() function只返回文件的文件名。

要正确排序,请在获得$images后添加此行:

usort($images,function($a,$b){
    $a=(int)preg_replace('~\D~','',$a);
    $b=(int)preg_replace('~\D~','',$b);
    return $a<$b?-1:1;
});