繁体   English   中英

如何使用 session 更新用户配置文件图像?

[英]How to update the user profile image using session?

我想更新我的个人资料图片,但我不知道如何更新它。 我成功地将它插入到我的数据库和文件夹中,但是当用户想要更新它时,所以我不知道如何更新这个图像。 你能告诉我这是怎么可能的吗? 谢谢。

我的 HTML 表格的代码

<form class="form" action="accountsetting.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-12 col-sm-auto mb-3">
<div class="mx-auto" style="width: 140px;">
<div class="rounded-circle avatar avatar-xl mb-3">
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php echo 
$_SESSION['user']['avatar']; ?>"  width="100" height="100">
</div>
</div>
</div>
<div class="col d-flex flex-column flex-sm-row justify-content-between mb-3">
<div class="text-center text-sm-left mb-2 mb-sm-0">
<h4 class="pt-sm-2 pb-1 mb-0 text-nowrap"><?php echo $_SESSION['user']['fullname']; ?></h4>
<p class="mb-0"><?php echo $_SESSION['user']['username']; ?></p>
<div class="form-group mb-2 pt-2">
<input class='input' type='hidden' name='id' value="<?php echo $_SESSION['user']['id']; ?>" />
<input id="avatar" type="file" name="avatar" hidden="" accept="image/png, image/jpeg, 
image/jpg">
<button id="uploadBtn" type="submit" name="profile" class="btn btn-primary btn-file " ><i 
class="fa fa-camera" aria-hidden="true"></i> &nbsp Upload Avatar</button>
</div>
<div class="row">
<div class="col d-flex justify-content-end pr-5">
<button class="btn btn-primary " type="submit" name="profileupdate">Save Changes</button>
</div>
</div>
</div>
</div>
</div>
</form>

我的 PHP 端的代码

// sava image into directoray
$id="";
$filename = $avatar['name'];
$fileerror = $avatar['error'];
$filetmp = $avatar['tmp_name'];
$fileext = explode('.',$filename);
$filecheck = strtolower(end($fileext));
$fileextstored =array('png', 'jpg', 'jpeg');
$destinationfile = 'upload/'; 
$destinationfile = $destinationfile .$filename;
$destinationfile1 = 'userside/upload/'; 
$destinationfile1 = $destinationfile1 .$filename;
try {
//throw exception if can't move the file
if (!move_uploaded_file($filetmp, $destinationfile)) {
throw new Exception('Could not move file');
}
if(!copy ( $destinationfile , $destinationfile1 ))
    {  
      throw new Exception('Could not move 2nd file');
    }
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}

// image insert
if(in_array($filecheck, $fileextstored))
{
$query = "INSERT INTO users (avatar) VALUES('$destinationfile')";
   $displayquery = "select * from users where id= $id";
   $displayquery = mysqli_query($db,$displayquery);  
}

我将使用什么类型的代码。 当用户单击保存按钮时,图像将成功更新。

看起来您在 INSERT INTO 语句中需要一个条件。 你不应该在id = $id的users表中插入头像吗?

这里:

"INSERT INTO users (avatar) VALUES('$destinationfile')";

改成:

"INSERT INTO users (avatar) VALUES('$destinationfile') WHERE id = $id";

更新:如果您要插入图像的路径,那么您可能需要将 URL 与图像标签一起放入:

<!-- insert code for http or https:// -->
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php  echo "http://" . $_SERVER['SERVER_NAME'] . "/" . $_SESSION['user']['avatar']; ?>"  width="100" height="100">

这取决于该路径是否有效并且可以从服务器名称访问,例如 http://localhost/uploads/image_name.jpg 如果路径是.../uploads/image_name.jpg。 您可以将路径放在 session 变量中,然后在末尾添加图像名称,例如 $_SESSION['image_path'] = 'http://localhost/uploads/'。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM