PHP默认个人资料图片设置

3

我在创建if语句时遇到了问题,目的是在用户没有上传自己的个人资料照片的情况下显示默认个人资料照片。我尝试了许多在线示例,但似乎都没有适用于我的特定代码,并且没有回复工作状态。我删除了尝试解决问题的尝试,并恢复了原始代码。这是我目前拥有的代码:

<?php
    session_start();

    if (!isset($_SESSION['username'])) {
        $_SESSION['msg'] = "You must log in first";
        header('location: login.php');
    }

    if (isset($_GET['logout'])) {
        session_destroy();
        unset($_SESSION['username']);
        header("location: login.php");
    }
$username = $_SESSION['username'];
$file = "extra/" . $username . ".png";
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
    <div class="container">
      <nav>
        <ul>
          <li><a href="index.php">Home</a></li>
          <li><a href="downloads.php">Downloads</a></li>
          <li><a href="chat.php">Chat</a></li>
          <li><a href="profile.php">Profile</a></li>
          <li class="logout"><a href="index.php?logout='1'">Logout</a></li>
        </ul>
      </nav>
    </div>
  </header>
<body>
  <div class="profileIMG">
    <img src=<?php echo $file; ?>  width="100" height="100">
  </div>
  <div class="profileNAME">
    <<?php echo $username ?>
  </div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>

CSS

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 13%;
    background-color: #f1f1f1;
    position: fixed;
    height: 100%;
    font-size: 20px;
    font-family: arial;
    overflow: hidden; 
}
li a {
    display: block;
    color: #000;
    padding: 8px 16px;
    text-decoration: none;
}

li a.active {
    background-color: #4CAF50;
    color: white;
}

li a:hover:not(.active) {
    background-color: #555;
    color: white;
}

li a:target { 
    background-color: #4CAF50;
    color: white;
}

li button {
    display: block;
    color: #000;
    padding: 8px 16px;
    text-decoration: none;
}

li button.active {
    background-color: #4CAF50;
    color: white;
}

li button:hover:not(.active) {
    background-color: #555;
    color: white;
}
body { 
    background-image: url(extra/background.png); 
    margin: 0;
    overflow: hidden;
}
h5 {
    color: green;
    margin-left: 6%;
    font-family: arial;
    font-size: 15px;
}
input[type=text] {
    width: 100%;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-position: 10px 10px;
    background-repeat: no-repeat;
    padding: 12px 20px 12px 15px;
}
#player {
  width: 200px;
  height: 50px;
  position: relative;
  margin-left: 24px;
  top: 18px;
}
#player i {
  position: absolute;
  margin-top: -6px;
  color: #666;
}
#player i.fa-volume-down {
  margin-left: -8px;
}
#player i.fa-volume-up {
  margin-right: -8px;
  right: 0;
}

#volume {
  position: absolute;
  left: 24px;
  margin: 0 auto;
  height: 5px;
  width: 150px;
  background: #555;
  border-radius: 15px;
}
#volume .ui-slider-range-min {
  height: 5px;
  width: 300px;
  position: absolute;
  background: #2ecc71;
  border: none;
  border-radius: 10px;
  outline: none;
}
#volume .ui-slider-handle {
  width: 20px;
  height: 20px;
  border-radius: 20px;
  background: #FFF;
  position: absolute;
  margin-left: -8px;
  margin-top: -8px;
  cursor: pointer;
  outline: none;
  z-index: 1;
}
.logStatus {
  position: absolute;
  bottom: 0;
}

CSS并非必需,但可以使页面更加美观。此外,文件夹extra/包含图片,默认照片的名称为defaultProfile.png

1个回答

3
在你初始化$file变量的脚本顶部进行这个简单的更改。
$file = 'extra/' . $username . '.png';

if (!file_exists($file))
    $file = 'path-to-default-image';

公平地说,如果你想保持你的代码“逻辑性”,你可以翻转上面的检查,首先将$file设置为默认图像位置,然后如果用户的图像存在,则切换到该图像:

$file = 'path-to-default-image';

if (file_exists('extra/' . $username . '.png'))
    $file = 'extra/' . $username . '.png';

阅读材料

file_exists


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接