我无法将图像上传到MySQL并将其保存为BLOB格式。我知道有不同的方法来保存图像但是对于这个项目我需要这样做....这是我正在尝试的代码:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit_profile').on('click', function() {
$.ajax({
type: "POST",
url: 'updateProfile.php',
dataType: "html",
data: {
//that way it just dont work
//I've seen some examples where they use
// var formData = new FormData($('#updateProfile'));
userID: <?php echo $_SESSION['userID']; ?>,
image: $('#image').val(), // This didn't work
city: $('select[name=city]').val(),
desc: $('#desc').val()
},
success: function(data) {
$('.resultDiv').html(data);
}
)};
)};
)};
</script>
</head>
<body>
<form method="POST" id="updateProfile" enctype="multipart/form-data">
Image: <input type="file" name="image" id="image" accept="image/*"><br>
City: <select name="city">
<option value="Moscow">Moscow</option>
<option value="New York">New York</option>
</select>
Description: <textarea name="desc" id="desc"></textarea>
</form>
<button id="submit_profile" class="submit">Update</button>
</body>
</html>
PHP方面:
<?php
session_start();
require ("db.php");
if($_POST['userID'] == $_SESSION['userID']) {
$userID = $_POST['userID'];
// Image
$city = trim(mysqli_escape_string($conn, $_POST['city']));
$desc = trim(mysqli_escape_string($conn, $_POST['desc']));
$errorinfo = $_FILES["userImage"]["error"];
$filename = $_FILES["userImage"]["name"];
$tmpfile = $_FILES["userImage"]["tmp_name"];
$filesize = $_FILES["userImage"]["size"];
$filetype = $_FILES["userImage"]["type"];
//
if (!($filetype == "image/jpeg" && $filesize > 0)) {
echo "<script>alert('Import of photo failed')</script>";
} else if($filesize < 0 && $filesize > 5120) {
echo "<script>alert('Import of photo failed!')</script>";
} else {
// Update query
}
} else {
echo "<script>window.location='/index.php';</script>";
}
?>
有人可以给我一个符合我需求的工作示例。提前谢谢!