【发布时间】:2015-03-11 23:17:23
【问题描述】:
当存储在数据库中的图像的文件路径条目为空时,我正在尝试在 cakePHP 中显示默认图像。我发现下面的代码正在发生,如果数据库条目为空,则显示默认图像,但如果数据库中有条目,它也会显示。
因此,由于某种原因,如果 DB 中有条目 $image 没有被显示。感谢您的帮助。
保罗
<?php
$image = $this->Html->image(
$news['News']['imgPath'],
array('title' => $news['News']['alt_tag'], 'class' => 'left'),
array('escape' => false));
$default_image = "<img src=\"/FBCW_new2/files/uploads/default.jpg\" class=\"left\" alt=\"default image\"/>";
if(file_exists("$image")) $filename = $image;
else $filename = $default_image;
echo $filename;
?>
分辨率: 由于$image是html字符串,所以我添加了一个变量,首先检查文件路径是否为空,然后通过它设置$filename。
$photo = $news['News']['imgPath'];
$image = $this->Html->image(
$news['News']['imgPath'],
array('title' => $news['News']['alt_tag'], 'class' => 'left'),
array('escape' => false));
$default_image = "<img src=\"/FBCW_new2/files/uploads/default.jpg\" class=\"left\" alt=\"default image\"/>";
if(!empty($photo)) $filename = $image;
else $filename = $default_image;
echo $filename;
【问题讨论】:
标签: php image cakephp file-exists