只使用PHP中的部分文件名读取图像


Reading an image in PHP using only part of its file name

我试图通过只获取PHP目录中的部分名称来读取存储在该目录中的图像。图像命名如下,

AT-1410f1654.jpg
AT-1410_1655.jpg
AT-1410_1656.jpg
AT-1410_1657.jpg
AT-1410_1658.jpg
AT-1410_1659.jpg

我尝试了下面的代码,但即使我使用PHP substr()方法,它也不起作用,但它也不工作,

    $dbImage=$row["pref"];
    $imageName=$dbImage;
    $extension=".jpg";
    $filename=$imageName.$extension;
    echo "$filename+<img src='proppics/$filename'>"; 

关于如何做到这一点的任何想法

全代码

$limit=10; 
$con=mysql_connect("localhost","root","");
mysql_select_db("movedb") or die("Unable to select database");
$query="select * FROM properties where `name`='Beata Grande 1' & `catergory`='Villas'& `price`=202800 & `area`='Arenas'& `bedrooms`=2 & `region`='Axarquia'";
$numresults=mysql_query($query,$con);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
echo "<center>";
echo "<p>You searched for: &quot;" . $properties . "&quot;</p>";
echo "<form name=payment action='properties_details.php'>";
echo "Results <br>";
  while ($row= mysql_fetch_array($result)) {
    $id=$row['id'];
    $pid=$row['pref'];
    // Retrieve the balance database fields
      echo "<p>Property ID &nbsp".$pid;
      echo "<br> <p> Name &nbsp";
      echo $row["name"];
      echo "<br>  Properties &nbsp";
      echo $row["catergory"];
      echo "<br> Description &nbsp";
    // Print results
 echo "<br>";
 echo "<input type=submit name=btnbuy value=MoreDetails> "; 

首先,使用scandir 列出目录中的所有文件

http://php.net/manual/en/function.scandir.php

然后使用strpos 检查现有前缀

http://php.net/manual/en/function.strpos.php

$dir    = 'propix';
$files  = scandir($dir);
$prefix = '1410';
foreach($files as $file) {
    if(strpos($prefix) !== FALSE) { //here you are supposed to add more...
        echo $file;
    }
}

glob怎么样?

您的问题不够清楚,无法提供更全面的答案

我不能100%确定你想要实现什么。但是,无论出于何种原因,如果您只想在"_"之后或之前选择文件名的一部分,可以使用PHPs explode()函数。

在你的情况下写:

print explode("_", $dbImage);

会有类似的输出:

Array(
    [0] => AT-1410
    [1] => 1655.jpg
)