PHP默认配置文件图片设置

时间:2018-04-10 14:37:29

标签: php html

我在创建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 个答案:

答案 0 :(得分: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