简体   繁体   English

使用PHP从完整图像路径获取图像名称

[英]Get Image name from full image path using PHP

I am trying to display an image with a caption and thumbnails. 我正在尝试显示带有标题和缩略图的图像。 I am able to display the picture but I am finding difficult to display the image file name as caption. 我可以显示图片,但发现很难将图像文件名显示为标题。

Below is my code: 下面是我的代码:

<?php
$files = glob("images/Friends/*.*");
for ($i=0; $i<count($files); $i++)
{
    $num = $files[$i]; 
    echo '<div class="w3-content" style="max-width:800px;position:relative;padding-top:0px;">';
    echo '<div class="w3-display-container mySlides">';
    echo '<div class="col s12 m6 l12">';
    echo '<div class="page4">';
    echo '<img src="'.$num.'" height="750" width="780" alt="random image" class="responsive-img" >'."&nbsp;&nbsp;";    
    echo '</div>';
    echo '</div>';
    echo '<a class="w3-btn-floating w3-hover-dark-grey"    style="position:absolute;top:45%;left:0" onclick="plusDivs(-1)">❮</a>';
    echo '<a class="w3-btn-floating w3-hover-dark-grey" style="position:absolute;top:45%;right:0" onclick="plusDivs(1)">❯</a>';
    echo '</div>';
    echo ' </div>';
}
?>  

<script>
    var slideIndex = 1;
    showDivs(slideIndex);
    function plusDivs(n) {
        showDivs(slideIndex += n);
    }
    function showDivs(n) {
        var i;
        var x = document.getElementsByClassName("mySlides");
        if (n > x.length) {slideIndex = 1}
        if (n < 1) {slideIndex = x.length}
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }
        x[slideIndex-1].style.display = "block";
    }
</script>

To show caption set title attribute. 要显示字幕,请设置标题属性。 And set name of image in tile attribute. 并在tile属性中设置图像名称。

To get Image name from fullpath of Image use basename($num) 要从Image的全路径获取图像名称,请使用basename($num)

Please replace your php code with below. 请使用以下代码替换您的php代码。

echo '<img src="'.$num.'" height="750" width="780" alt="'.basename($num).'" title="'.basename($num).'" class="responsive-img" >'."&nbsp;&nbsp;"; 

To get filename without extension for every image place below code in loop. 要获取文件名不带扩展名的循环图中下面的每个图像。 And place $filename variable where you want to display image name. 并将$filename变量放在要显示图像名称的位置。

$filenamewithextension = basename($num);
$fileextenstion = pathinfo($filenamewithextension, PATHINFO_EXTENSION);
$filename = basename($num,$fileextenstion);
$filename = trim(preg_replace('/[0-9]+/', '', $filename));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM