简体   繁体   English

从php中的文件目录中删除文件扩展名

[英]remove file extensions from a file directory in php

I am doing a gallery and using php to get images to show up without hard-coding for each image. 我正在做一个画廊,并使用php来获取图像而无需为每个图像进行硬编码。 I had used the following method for obtaining the images: 我使用以下方法获取图像:

<?php
$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
    //readdir

    while($file=readdir($opendir))
    {
        if ($file!="."&&$file!="..")
    echo "
    <div id='item' class='grid-item'>
    <a href='$dir/$file'>
    <div id='masks' class='mask'></div>
    <div id='title' class='text'>$file</div>
    <img src='$dir/$file'>
    </a>
</div>

";
    }
 }
 ?>

The gallery uses jquery for the masonry gallery plugin. 画廊使用jquery作为砌体画廊插件。

However the file extension is showing up along with the file name. 但是,文件扩展名和文件名一起显示。 I don't want the extension. 我不要扩展名。 I have tried many methods, but they're not working for this case. 我尝试了很多方法,但不适用于这种情况。 How can I hide it and what changes can be done to the code in order for it to happen? 如何隐藏它以及可以对代码进行哪些更改以使其发生?

You can use pathinfo : 您可以使用pathinfo

<?php

$dir="image/";
//open dir
if ($opendir=opendir($dir))
{
    //readdir

    while($file=readdir($opendir))
    {
        if ($file!="."&&$file!="..")
        {
            $filename = pathinfo($file, PATHINFO_FILENAME);
            echo "
    <div id='item' class='grid-item'>
    <a href='$dir/$file'>
    <div id='masks' class='mask'></div>
    <div id='title' class='text'>$filename</div>
    <img src='$dir/$file'>
    </a>
</div>

";
        }

    }
}

To get the filename via this method, you must use PHP > 5.2, which you probably should anyway. 要通过此方法获取文件名,您必须使用PHP> 5.2,无论如何您都应该这样做。

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

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