PHP:图像不会显示

时间:2015-09-15 12:42:22

标签: php image

我有一个将数据上传到数据库的表单,其中包括上传图像的目录的路径。一切都有效,除了图像不会显示的事实。

在我的浏览器中查看来源告诉我图像已找到,但我不断收到损坏的图像图标。

这是我的代码:

$dir = "../uploaded_images/";

$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);

if (count($fileArray) > 0) {
   $image = $fileArray[0];
   echo "<img src='$image' width='300px'>";
}

在表单中,您可以上传多个文件。在数据库中,文件获得随机前缀,然后是文件名,如3456456745654_imageName.jpg。

如果上传了多个文件,它们会用星号(*)拆分,这就是我爆炸的原因。

然后,要仅打印一张图像,我会检查与特定记录相关的图像数量,然后仅显示第一张图像。

PS。此代码适用于显示与所选图像相关的所有图像:

$dir = "uploaded_images/";

$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);

foreach ($fileArray as $file) {
   if (file_exists($dir . $file)) {
      echo "<img class='images' src='$dir/$file' width='300px;'>";
   }
}

但这是针对不同页面显示所选车辆信息的信息,包括所有图片。

我需要在着陆页面上每辆车只显示一张图片,列出所有车辆。

1 个答案:

答案 0 :(得分:0)

必须使用目录:

$dir = "../uploaded_images/";

$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);

if (count($fileArray) > 0) {
   $image = $fileArray[0];
   if (file_exists($dir . $image)) {
      echo "<img class='images' src='$dir/$image' width='300px;'>";
   }
 }