0
我一直在努力,允許用戶上傳個人資料圖片。圖爲當你選擇了它,但是當你按下保存按鈕,它dissappears並沒有上傳到數據庫中。上傳的圖片不保存到數據庫
這是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>
功能
您公開的PHP代碼沒有顯示任何嘗試上傳到數據庫。我懷疑它的這個調用中:'$ user2-> setProfileImage($ profileImage);'你可能想分享'User'類代碼。 – Bing