dongmi1221 2016-09-30 10:43
浏览 625
已采纳

使用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>
  • 写回答

1条回答 默认 最新

  • dongmi4720 2016-09-30 10:49
    关注

    To show caption set title attribute. And set name of image in tile attribute.

    To get Image name from fullpath of Image use basename($num)

    Please replace your php code with below.

    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.

    $filenamewithextension = basename($num);
    $fileextenstion = pathinfo($filenamewithextension, PATHINFO_EXTENSION);
    $filename = basename($num,$fileextenstion);
    $filename = trim(preg_replace('/[0-9]+/', '', $filename));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致