我一直致力于允许用户上传个人资料照片。图片显示您选择它时,但当您按下保存按钮时,它会消失并且不会上传到数据库。
这是php代码
if (!empty($_FILES) && isset($_POST['addPicture'])) {
$file = $currentUser['userID'];
$imageType = pathinfo(basename($_FILES["profile_picture"]["name"]), PATHINFO_EXTENSION);
$targetFile = "upload/profilePics/" . $file . "." . $imageType;
try {
if ($imageType != "jpg" && $imageType != "png" && $imageType != "jpeg" && $imageType != "gif") {
throw new Exception('Dit is geen afbeelding');
}
$profileImage = "upload/profilePics/".$currentUser['userID'].".".$imageType;
try{
$user2 = new User();
$user2->setEmail($_SESSION['user']);
$user2->setProfileImage($profileImage);
if ($user2->changePicture()){
$currentUser= $user->getProfile();
$feedback2 = "Saved";
}
} catch (Exception $e) {
$error2 = $e->getMessage();
}
} catch (Exception $e) {
$error2 = $e->getMessage();
}
}
这是功能
public function changePicture(){
$conn = Db::getInstance();
$update = $conn->prepare("UPDATE users SET profileImage = :profileImage WHERE email = :email");
$update->bindValue(':profileImage', $this->profileImage);
$update->bindValue(':email', $this->email);
return $update->execute();
}
这是html代码
<form class="addPic" action="" method="post" enctype="multipart/form-data">
<div class="imageContainer profilePic" style="background: url('<?php echo $currentUser["profileImage"] ?>') center;background-size: cover;"></div>
<label for="profile_picture"></label>
<input type="file" name="profile_picture" id="profile_picture" onchange="readURL(this);" class="form-control">
<input name="addPicture" class="editPic" type="submit" value="Aanpassen">
</form>
最后,这是javascript
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.profilePic').attr('style', "background: url('"+e.target.result+"') center;background-size: cover;");
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
答案 0 :(得分:0)
确保图像字段的数据类型设置为BLOB.BLOB是“二进制大对象”,用于存储大量二进制数据,如图像或其他类型的文件。
在数据库中保存图像不是一个好习惯。 将图像保存在某个文件夹中并将路径保存在数据库中。