如何从一个文件夹一个接一个地获取图片,并显示在一个页面上与特定的文本为每个图像使用php


How to get the pictures from a folder one by one and display them on a page with specific text for each image using php?

我使用此代码在我的网页上添加产品…

<table align="center" border="0" cellpadding="0" cellspacing="0" width="500">
<tr>
<td width="250" >  <a href="img/01.JPG" rel="lightbox" ><img src="img/01a.JPG" border="0"   width="200" height="150"><br><br><br><br></a>   </td>
<td width="250" >
<center>
<span class="style2">Name of the product</span><br>
Description of the product
<br><br>
<b></b><br><br><br><br><br>
</center>
</td>
</tr>
</table>

我不想一遍又一遍地重写这段代码…我想要一个代码,将从一个文件夹中获得产品的图像,并为每个产品显示一个特定的文本。

所有产品的文本将放在与图像编号相同的文件夹中(图像编号01,文本编号01,图像编号02,文本编号02等)。

如果产品没有文本文件,它将显示(产品的图像),但不显示文本。

在images文件夹中,我有同一产品的图片和缩略图(01.jpg-image, 01a.jpg-thumbnail image, 02.jpg-image, 02.jpg- thumbnail image等)

提前感谢:)

声明$filepath并尝试使用这段代码:

<?
$string =array();
$filePath='directorypath/';  
$dir = opendir($filePath);
while ($file = readdir($dir)) { 
   if (eregi("'.png",$file) || eregi("'.jpg",$file) || eregi("'.gif",$file) ) { 
   $string[] = $file;
   }
}
while (sizeof($string) != 0){
  $img = array_pop($string);
  echo "<img src='$filePath$img'  width='100px'/>";
}
?>

也可以看看 PHP官方网站

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

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

http://php.net/manual/en/refs.utilspec.image.php

按照下面列出的步骤将其设置为动态

步骤1:创建名为images的表

create table `images` (
`id` int(4)auto_increment,
`imagename` varchar(30),
`description` varchar(100),
PRIMARY KEY (`id`)
);

步骤2:创建连接文件conf.php

<?php
$con=mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

步骤3:创建image.php文件

<?php
include ('config.php');
$sql="select image, description from images";
$sql_res=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($sql_res,MYSQLI_ASSOC))
{
?>
<img src="folderpath/<?php echo $row["images"];?> />
Description 
<?php
echo $row['description'];
?>

完成后,将连接文件(config.php), images.php和images文件夹放在同一目录下,并运行images.php一次,为目录路径,数据库名称,用户名,密码等提供适当的值。

一旦完成,你的目标就会实现。