在PHP中显示文件夹中的图像

时间:2022-02-10 06:08:06

I am currently creating an e-commerce website where I retrieve the vehicle details from the database and search for its image in a folder. I used the code below for searching the folder for the image, but it only show the name of the JPG file and not the image of it. Could anyone help to to determine how I could display the image of the vehicle

我目前正在创建一个电子商务网站,我从数据库中检索车辆详细信息并在文件夹中搜索其图像。我使用下面的代码搜索图像的文件夹,但它只显示JPG文件的名称而不是它的图像。任何人都可以帮助确定如何显示车辆的图像

$imageName=$_POST['car'];
if ($handle = opendir('Images/$imageName')) {
   $dir_array = array();
   while (false !== ($file = readdir($handle))) {
  if($file!="." && $file!=".."){
$dir_array[] = $file;
}
}    
echo $dir_array[rand(0,count($dir_array)-1)];
 closedir($handle);
}

3 个解决方案

#1


2  

You have to put html tags around the result. Try:

你必须在结果周围放置html标签。尝试:

echo "<img src='PATH_TO_IMAGE/".$dir_array[rand(0,count($dir_array)-1)]."'>";

echo“在PHP中显示文件夹中的图像“;

where PATH_TO_IMAGE is the relative/absolute url address to your images folder

其中PATH_TO_IMAGE是images文件夹的相对/绝对URL地址

Ex

echo "<img src='/images/".$dir_array[rand(0,count($dir_array)-1)]."'>";

echo“在PHP中显示文件夹中的图像“;

#2


2  

You need to print the path to the image inside an img HTML tag, otherwise it is just plain text.

您需要在img HTML标记内打印图像的路径,否则它只是纯文本。

echo '<img src="Images/' . $imageName . $dir_array[rand(0,count($dir_array)-1)] . '" alt="" />';

I hope you're doing some validation/sanitisation on $_POST['car'] somewhere?

我希望你在某个地方对$ _POST ['car']进行一些验证/消毒?

#3


0  

Hmm... If you have the name of the JPG, use <img src="NAME_OF_THE_IMAGE.JPG" /> ...

嗯......如果您有JPG的名称,请使用在PHP中显示文件夹中的图像 ...

#1


2  

You have to put html tags around the result. Try:

你必须在结果周围放置html标签。尝试:

echo "<img src='PATH_TO_IMAGE/".$dir_array[rand(0,count($dir_array)-1)]."'>";

echo“在PHP中显示文件夹中的图像“;

where PATH_TO_IMAGE is the relative/absolute url address to your images folder

其中PATH_TO_IMAGE是images文件夹的相对/绝对URL地址

Ex

echo "<img src='/images/".$dir_array[rand(0,count($dir_array)-1)]."'>";

echo“在PHP中显示文件夹中的图像“;

#2


2  

You need to print the path to the image inside an img HTML tag, otherwise it is just plain text.

您需要在img HTML标记内打印图像的路径,否则它只是纯文本。

echo '<img src="Images/' . $imageName . $dir_array[rand(0,count($dir_array)-1)] . '" alt="" />';

I hope you're doing some validation/sanitisation on $_POST['car'] somewhere?

我希望你在某个地方对$ _POST ['car']进行一些验证/消毒?

#3


0  

Hmm... If you have the name of the JPG, use <img src="NAME_OF_THE_IMAGE.JPG" /> ...

嗯......如果您有JPG的名称,请使用在PHP中显示文件夹中的图像 ...