【发布时间】:2018-07-07 11:38:20
【问题描述】:
我想在登录时显示我的用户个人资料图片。当他们单击change profile picture 按钮时,他们的图片将保存到目录中,然后保存到数据库中。我不知道从哪里开始。如何将用户拥有的任何图片显示为他们的个人资料图片?
profile.php:
<form id="form2" action="upload.php" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="change"><br />
</form>
<!-- Trigger the Modal -->
<img id="myImg" src="default.jpg" width="200" height="150">
编辑!!!!
<?php
$username = $_SESSION['username'];
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPG, JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"images/uploads/".$file_name);
$store=mysqli_query($conn,"UPDATE users SET userPic='$userPic' WHERE username='$username'");
mysqli_query($conn,$store);
echo "Success";
}else{
print_r($errors);
echo"it failed";
}
}
?>
<?php
$getimg = mysqli_query($conn,"SELECT userPic FROM users WHERE username='" .
$username . "'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['userPic'];
?>
<img id="myImg" src="images/uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150">
【问题讨论】:
-
从数据库中选择您需要的用户信息(id、用户名、头像等),可选择将其存储在会话中,然后使用该数据将其显示在页面上..?
-
这就是我显示用户名
<center><h5><?php echo $_SESSION['username']; ?></h5></center>的方式 -
有类似的吗?
-
假设您做事正常并将图像名称存储在数据库中,并将图像存储在文件系统中某个公开可用的地方,那么是的。
<img src="/images/profile_pictures/<?= $_SESSION['profile_picture'] ?>"/>之类的东西应该可以工作。请注意,您当然必须确保图像的路径正确匹配,但是您拥有它 -
您在会话中将 userPic 存储在哪里?
标签: php html mysqli phpmyadmin