i want to upload an image over Ajax and PHP. To get the Image i use FormData. The Ajax call is working fine from my point of view, but in my PHP code I always get the Error
Fatal error Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\fitnesslaeufer-redesign\profile\includes\settings\profile-settings-update-image.php:43
Stack trace: #0 C:\xampp\htdocs\fitnesslaeufer-redesign\profile\includes\settings\profile-settings-update-image.php(43): getimagesize('')
#1 C:\xampp\htdocs\fitnesslaeufer-redesign\profile\includes\settings\profile-settings-update-image.php(2): update_user_image()
#2 {main} thrown in C:\xampp\htdocs\fitnesslaeufer-redesign\profile\includes\settings\profile-settings-update-image.php on line 43
"
when I want to get informations ($fileinfo) about the image that is uploaded.
I tried to find a solution in some Posts but nothing helped to solve my problem.
For the Background I know how to upload an Image with PHP and now I want to update my image upload to Ajax / JS so that I can use SweetAlert2.
PHP Code:
if(isset($_GET['user_id'])) {
$user_id = mysqli_real_escape_string($connection, $_GET['user_id']);
}
$get_user_information = $connection->prepare("SELECT user_id, user_username, user_logo FROM user WHERE user_id = ?");
$get_user_information->bind_param("s", $user_id);
$get_user_information->execute();
$result = $get_user_information->get_result();
$row = $result->fetch_assoc();
$user_id = $row['user_id'];
$user_username = $row['user_username'];
$user_logo_old = $row['user_logo'];
$get_user_information->close();
if($user_id > 0) {
$targetDir = "../assets/img/users/";
$allowed_image_extensions = array("png","jpg","jpeg","PNG","JPG","JPEG");
if(isset($_FILES['user_logo']) && !empty($_FILES['user_logo']['name'])) {
$fileinfo = @getimagesize($_FILES['user_logo']['tmp_name']);
$width = $fileinfo[0];
$height = $fileinfo[1];
$user_image = pathinfo($_FILES['user_logo']['name'], PATHINFO_EXTENSION);
if(!in_array($user_image, $allowed_image_extensions)) {
echo "extension";
exit;
} else if(($_FILES['user_logo']['size'] > 1048576)) {
echo "large";
exit;
} else if($width > "500" || $height > "500") {
echo "size";
exit;
} else {
unlink("../assets/img/users/$user_logo_old");
$user_logo_new = $user_id.$user_username;
}
move_uploaded_file($_FILES['user_logo']['tmp_name'], $targetDir.$user_logo_new.".".$user_image);
$user_logo_path = $user_logo_new.".".$user_image;
$update_user_informations = $connection->prepare("UPDATE user SET user_logo = ? WHERE user_id = ?");
$update_user_informations->bind_param("ss", $user_logo_path, $user_id);
$update_user_informations->execute();
$update_user_informations->close();
echo 1;
exit;
} else {
$get_user_logo = $connection->prepare("SELECT user_logo FROM user WHERE user_id = ?");
$get_user_logo->bind_param("s", $user_id);
$get_user_logo->execute();
$result = $get_user_logo->get_result();
$row = $result->fetch_assoc();
$user_logo_path = $row['user_logo'];
$get_user_logo->close();
$update_user_informations = $connection->prepare("UPDATE user SET user_logo = ? WHERE user_id = ?");
$update_user_informations->bind_param("ss", $user_logo_path, $user_id);
$update_user_informations->execute();
$update_user_informations->close();
echo 0;
exit;
}
echo 0;
exit;
}
JS Code cutout:
....
const user_id = getCookie('user_id');
var formData = new FormData();
var file = $('#user_logo');
formData.append('file', file);
$.ajax({
url: "../profile/includes/settings/profile-settings-update-image.php?user_id=" + user_id,
method: "POST",
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(response) {
if(response == 1) {
Swal.fire({
title: "Profile picture is changed!",
type: "success",
closeOnConfirm: true
});
} else {
Swal.fire({
title: "Something went wrong!",
type: "error",
closeOnConfirm: true
});
}
}
})
....
source https://stackoverflow.com/questions/68975931/js-formdata-image-upload-to-php-image-can-not-be-uploaded
Comments
Post a Comment