我如何让PHP显示用户图片,如果你有一个在博客文章


How do I get PHP to display a user picture if you have one within a blog post?

我有一个可以显示用户图片的脚本,但是我想现在就有它,所以如果你没有图片,它会显示一个默认的。

if (file_exists($displayuserimage)) {
    $displayuserimage =  '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2"  />';
} else {
    $displayuserimage =  '<img src="upic/default.jpg" width="50px" height="50px" border="2"/>'; 
}

这是我的猜测,但这是工作脚本,显示图像如果你有一个:

$displayuserimage = '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2"/>';

您的if正在检查以前未定义的变量$displayuserimage。为了检查您的图像是否存在,if可能看起来像

if (file_exists("upic/". $posts['userid'].".jpg")) {

使用PHP的file_exists()函数

http://php.net/manual/en/function.file-exists.php

$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";
if(file_exists($image))
{
  echo "<img src='"$http_image'"/>'n";
}
else
{
  echo "<img src='"uploads/$username/noprofile.png'"/>'n";
} 

如果您也从数据库调用用户图像,请使用相同的场景。替换

echo "<img src='"$http_image'"/>'n";

与数据库中包含用户配置文件图像的行。例子:

echo $行[' user_image '],

相关文章: