我正在使用php脚本从文件夹中获取所有图像并对它们进行分页并且一切正常,但是我想使用实际照片名称(filemname)作为alt标记,但我发现很难得到这个名字。可以使用以下代码完成此任务:
<?php
//The directory to your images folder, with trailing slash
$dir = "cms/gallery/photo/";
//Set the extensions you want to load, seperate by a comma.
$extensions = "jpeg,jpg,gif,png";
//Set the number of images you want to display per page
$imagesPerPage = 3;
//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 "
<div class='galleryCellHolder'>
<div class='galleryCell'>
<a class='fancybox' rel='group' href='{$images[$i]}'><img class='galleryPhoto' src='{$images[$i]}' alt='I NEED THIS TO BE THE FILE NAME'></a>
</div>
</div>
";
}
}
//Now to display the page numbers!
for($p = 1; $p <= $totalPages; $p++){
if($p == $page){
$tmp_pages[] = "<strong class='pagination'>{$p}</strong>";
}else{
$tmp_pages[] = "<a class='pagination' href='?page={$p}'>{$p}</a>";
}
}
?>
答案 0 :(得分:0)
在你的循环中执行:
var_dump(basename($totalImages[$i]));
或者您也可以查看:
var_dump(pathinfo($totalImages[$i]));
还会有basename,dirname,extension等。
答案 1 :(得分:0)
使用basename()
。
$filename = explode('.', basename($images[$i]));
echo "
<div class='galleryCellHolder'>
<div class='galleryCell'>
<a class='fancybox' rel='group' href='{$images[$i]}'><img class='galleryPhoto' src='{$images[$i]}' alt='" . $filename[0] . "'></a>
</div>
</div>
";