在 php 中的图像下方显示图像名称

Displaying image name below the images in php

这是我用来制作画廊类型的代码:

主要 PHP :

<?php
$folder_path = 'images/'; //image's folder path

$num_files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);

$folder = opendir($folder_path);

if($num_files > 0)
{
    while(false !== ($file = readdir($folder))) 
    {
        $file_path = $folder_path.$file;
        $extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
        if($extension=='jpg' || $extension =='png' || $extension == 'gif' || 

$extension == 'bmp') 
        {
            ?>
            <a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"  

height="200" /></a>
            <?php
        }
    }
}
else
{
    echo "the folder was empty !";
}
closedir($folder);

我应该怎么做才能显示所有图像名称。请帮忙

检查这一行:

$file_path = $folder_path.$file;

这里$file是文件名,你可以用它来显示文件名,如:

echo $file;

根据您的要求是这样的:

<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"
<?php echo $file; ?>

while 循环中试试这个:

$file_path = $folder_path.$file;
$path_parts = pathinfo($file_path);
$extension = strtolower($path_parts['extension']);
if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
{
    echo '<a href="'.$file_path.'"><img src="'.$file_path.'" height="200" />'.$path_parts['filename'].'</a>';
}