Php 如果图像为空或已损坏,则添加常规图像

Php 如果图像为空或已损坏,则添加常规图像,php,file-exists,Php,File Exists,我有一个产品组合,每个产品组合都有一个图像,如果DB字段“image”为空,我将加载一个通用图像,但如果在某些情况下该字段有图像值,但文件不存在,我也希望使用通用图像,我尝试使用下面的代码,但仅适用于空字段: <div class="wrapper"><a href="store.php?url=<?php echo $row->url ?>"><img src="images/stores/category/ <?php

我有一个产品组合,每个产品组合都有一个图像,如果DB字段“image”为空,我将加载一个通用图像,但如果在某些情况下该字段有图像值,但文件不存在,我也希望使用通用图像,我尝试使用下面的代码,但仅适用于空字段:

<div class="wrapper"><a href="store.php?url=<?php echo $row->url ?>"><img src="images/stores/category/

<?php           
if (empty($row->image) or file_exists($row->image)==false) { 
    echo 'generic.jpg';
}else{                  
    echo $row->imagen; }?>" alt="<?php echo $row->seo ?>"/></a></div>        
    </div>
    <?php } ?>


谢谢你的时间

注意键入$row->imagen。 正如我看到的,$row->image只包含文件名。除非图片位于当前工作目录中,否则您需要文件_的完整路径:

file_exists('htdocs/path/to/my/images/' . $row->image)==false)
要了解所需的路径,请将一个php文件放入包含内容的pictures文件夹中

die(__FILE__);

致以最诚挚的问候

好的代码应该是这样的:

if (file_exists('/Library/WebServer/Documents/theimage.jpg')) {
  echo 'File exists';
} else {
  echo 'Does not exist';
}
注1:使用is_文件检查文件是否实际存在并可用

注意2:使用脚本到文件的相对路径,因为该字段仅包含文件名

注3:在$row->imagen上输入错误

<div class="wrapper"><a href="store.php?url=<?php echo $row->url ?>"><img src="images/stores/category/

<?php           
if (empty($row->image) || !is_file('images/stores/category/' . $row->image)) { 
    echo 'generic.jpg';
}else{                  
    echo $row->image; }?>" alt="<?php echo $row->seo ?>"/></a></div>        
    </div>
    <?php } ?>

文件\u的存在应指向文件系统中的某些内容

试着这样做:

if (file_exists('/Library/WebServer/Documents/theimage.jpg')) {
  echo 'File exists';
} else {
  echo 'Does not exist';
}

如果我适用于您的代码,则为:

<div class="wrapper"><a href="store.php?url=<?php echo $row->url ?>"><img src="images/stores/category/
if (empty($row->image) or !file_exists('./images/stores/category/'.$row->image)) { 
    echo 'generic.jpg';
}else{                  
    echo $row->imagen; }?>" alt="<?php echo $row->seo ?>"/></a></div>        
    </div>
<?php } ?>

我们几乎有同样的问题。但对我来说;如何无法使用显示数据库中的crock图像/空图像
”;

它不会工作。

这是一个输入错误
$row->imagen
?使用
Is_file
而不是
file_存在
。如果路径是目录,最后一个也会返回true。这很好!前面的答案也很好,到底有什么区别,我丢失了路径。谢谢你的答案!所有解决方案都有效,我丢失了多谢了,输入错误是翻译错误,这是真正的字段名,DB是西班牙语的。Thanks@Ph.T你能帮我吗?