1、个人中心显示当前登陆用户的信息和实现头像上传功能
源码如下:
<!DOCTYPE html>
<html>
<head>
<title>个人中心</title>
<meta charset="utf-8">
</head>
<body>
<?php
include "conn.php";
$uid = $_GET["uid"];//留言板页面通过session传过来的uid
//echo $uid;
$sql = "select * from user where uid=$uid";
$result = mysqli_query($conn,$sql);
$persion = mysqli_fetch_array($result);
?>
<center>
<form action="pic.php" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="4">
<tr> <!--设置文件头像上传-->
<td align="center" colspan="2"><?php echo "<img src='".$persion['picture']."' width='50' higth='50'>";?></td>
</tr>
<td colspan="2"><input type="file" name="file"><input type="submit" name="submit" value="提交"></td>
<input type="hidden" name="uid" value="<?php echo $uid;?>"></td>
<tr>
<td>用户名</td>
<td><?php echo $persion["username"]; ?></td><!--输出当前登陆用户的信息-->
</tr>
<td>邮箱</td>
<td><?php echo $persion["email"]; ?></td>
</tr>
<td>性别</td>
<td><?php echo $persion["sex"]; ?></td>
</tr>
<td>时间</td>
<td><?php echo date("Y-m-d");?></td>
</tr>
</form>
</table>
</body>
</html>
效果图:
2、头像上传
源码如下:
<meta charset="utf-8">
<?php
include "conn.php";
//echo $_FILES['file']['name'] . "<br/>";
//echo $_FILES['file']['type'] . "<br/>";
//echo $_FILES['file']['tmp_name'] . "<br/>";
//echo $_FILES['file']['size'] . "<br/>";
//echo $_FILES['file']['error'] . "<br/>";
$path = "./image/";
$uid = $_POST["uid"];
if (!is_dir($path))
{
mkdir($path);
}
$mime = $_FILES['file']['type'];
if (($mime == "image/jpeg") || ($mime == "image/gif") || ($mime == "image/png") && ($_FILES['file']['error'] == 0))
{
$filename = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $filename);
$sql = "update user set picture = '$filename' where uid = $uid";
$result = mysqli_query($conn,$sql);
if ($result)
{
echo "<script>alert('头像上传成功!')</script>";
echo "<script>window.location.href=\"pcenter.php?uid=$uid\"</script>";
}
}
else
{
echo "<script>alert('头像上传失败!请重试!')</script>";
echo "<script>window.location.href=\"pcenter.php?uid=$uid\"</script>";
}
mysqli_close($conn);
?>