У меня возникла проблема, из-за которой я не могу обновить данные своего пользователя без обновления изображения профиля. Если я нажму кнопку «Отправить» без загрузки какого-либо изображения, появится сообщение об ошибке, подобное приведенному ниже. Я могу обновить детали, если я также обновил фотографию профиля одновременно.
Вот мои коды для edit-profile-upload.php. Извините за длинные коды. Я думаю, что все одинаково важно в этом файле:
<?php
$target_dir = "../include/img/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if (isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false)
{
$uploadOk = 1;
$profile_pic = $_FILES["fileToUpload"]["tmp_name"];
$imgContent = addslashes(file_get_contents($profile_pic));
$sql = "UPDATE users SET email = :email, phone = :phone, address = :address, postal_code = :postal_code, state = :state, city = :city, country = :country,
profile_pic = '" . $imgContent . "' WHERE username = '" . $_SESSION['username'] . "'";
$insert_statement = $db->prepare($sql);
$insert_statement->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
$insert_statement->bindParam(':phone', $_POST['phone'], PDO::PARAM_STR);
$insert_statement->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
$insert_statement->bindParam(':postal_code', $_POST['postal_code'], PDO::PARAM_STR);
$insert_statement->bindParam(':state', $_POST['state_id'], PDO::PARAM_STR);
$insert_statement->bindParam(':city', $_POST['city_id'], PDO::PARAM_STR);
$insert_statement->bindParam(':country', $_POST['country_id'], PDO::PARAM_STR);
$insert_statement->execute();
if ($insert_statement)
{
echo "<script>alert('Your profile picture " . basename($_FILES["fileToUpload"]["name"]) . " has been changed.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
else
{
echo "<script>alert('Your profile picture failed to upload. Please try again.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
}
else
{
echo "<script>alert('File is not an image.');";
echo 'window.location= "../include/edit-profile.php"';
echo "</script>";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "<script>alert('Sorry your file is too big.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
{
echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
echo "<script>alert('Sorry your file was not uploaded.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "<script>alert('The file " . basename($_FILES["fileToUpload"]["name"]) . "has been uploaded.');";
echo 'window.location= "../include/edit-profile.php"';
echo '</script>';
}
}
?>
Вот часть формы.
<form class="form-horizontal" onSubmit="return formValidation();" method="POST" enctype="multipart/form-data" action="edit-profile-upload.php">
Вот часть загружаемой картинки:
<input type="file" class="btn btn-default" name="fileToUpload" id="fileToUpload">
А вот и кнопка отправки:
<button type="submit" name="submit" class="btn btn-success pull-right">Submit</button>