Im trying to update user image with php but im facing errors that
$_FILES
is not defined. Already is set but facing the same issue. I
have tried to find in other answers here but its not working.
File path is added on database without extension (example: photos/5f583778a818e.) but file is not moved to directory (folder).
I appreciate your help!
Image Directory: dms>views>photos
public function updateuser(){
GLOBAL $conn;
if(@$_POST['first'] && (@$_POST['last']) && (@$_POST['filepath']) && !empty($_POST['filepath'])){
//if(@$_POST['first'] && (@$_POST['last']) && (@$_POST['filepath']){
//}
if($_SESSION['id']){
$id = $_SESSION['id'];
}
$user = new UserModel();
//echo $id;
$user->first = trim($_POST['first']);
$user->last = trim($_POST['last']);
@$filetemp = isset($_FILES['filepath']['tmp_name']);
@$filename = uniqid(isset($_FILES['filepath']['name']));
@$filetype = end(explode('.', ($_FILES['filepath']['name'])));
$filename = $filename . '.' . $filetype;
$user->filepath = "photos/" . $filename;
move_uploaded_file($filetemp, $user->filepath);
//var_dump($_FILES);
//var_dump(is_dir('photos/')); check if folder is DIR
$query = $conn->prepare("UPDATE users SET first = ?, last = ?, filepath = ? WHERE id = '$id'");
$query->bindValue(1, $user->first);
$query->bindValue(2, $user->last);
$query->bindValue(3, $user->filepath);
$query->execute();
//print_r($_FILES);
echo "Updated";
}else{
echo "None";
}
}
Below is my html
<form method="POST" enctype="multipart/form-data">
<div class="main-img-user profile-user input--file"><img id="previewImg"
src="views/<?php echo $user->filepath; ?>" alt="user-img"
class="rounded-circle mCS_img_loaded"><a href="JavaScript:void(0);"
class="fas fa-camera profile-edit"></a>
<input id="filepath" name="filepath" type="file" onchange="previewFile(this);" />
</div>
<input type="submit" id="update" value="Update" class="btn btn-primary waves-effect waves-
light">
<h3 class="result" style="color:green;"></h3>
</form>
And my AJAX
$(document).ready(function () {
$('#update').click(function (e) {
e.preventDefault(); $.get('/myurl/')
var first = $('#first').val()
var last = $('#last').val()
// var password = $('#password').val()
var filepath = $('#filepath').val()
$.ajax({
url: 'profile/updateuser',
method: 'POST',
data: {
first: first,
last: last,
filepath: filepath
},
success: function (data, textStatus) {
$('.result').html(data).show().delay(6000).fadeOut(300)
}
})
})
})