I am loading an image file using jquery filedialog. And then sending an ajax request to a php file to upload the image to database. Here is my ajax request being sent
$("#imgfile").on("change",function(e){
var file_data = $('#imgfile').prop('files')[0];
e.preventDefault();
var form_data = new FormData();
form_data.append('file', file_data);
//alert(form_data);
$.ajax({
url: 'changeProfileImg.php', // point to server-side PHP script
contentType: "image/jpeg", // what to expect back from the PHP script, if anything
cache: false,
dataType: "image",
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});
Here is my php code
include_once 'dbconnect.php';
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
//move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
$file=$_FILES['file']['name'];
$id=$_SESSION['id'];
$sql="UPDATE mainadmin set photo='$file' where id='$id'";
mysql_query($sql);
echo 'success';
$_SESSION['photo']=$file;
}
But i see some garbase thing is being inserted into database not showing the image. How can i do it working.