我使用jQuery Ajax上传图像,我也希望用Ajax数据传递用户ID,但是当Ajax运行时,我得到了下面的错误。我正在使用MVC结构。
Notice: Undefined index: userImage in/var/www/propio/application/models/user_model.php on line 544
我不明白为什么我会收到错误。这是我的Ajax代码:
$.ajax({
url: BASE_URL+"user/updateUserImage",
type : "POST",
data : new FormData(this) + '&id=' + <?php echo $user_id; ?>,
contentType : false,
cache : false,
processData : false,
success : function(data) {
$('#loading-div').hide(); // Hide ajax loader
if(data == "Error: Please upload your image."){
$('#errorBox').html(data);
}else if(data == "Error: File is too large. File must be less than 2MB."){
$('#errorBox').html(data);
}else if(data == "Error: Image must be in JPEG, JPG or PNG format."){
$('#errorBox').html(data);
}else{
$("#change_photo").html(data);
$('.featherlight-close').click();
}
},
error : function() {
}
});
控制器:user.php
public function updateUserImage(){
// Load model, perform an action on the model
$loadOffice = $this->loadModel('user_model');
return $postData = $loadOffice->updateUserImage();
}
型号:user_model.php
public function updateUserImage(){
if($_FILES['userImage']['type'] =='image/jpeg' || $_FILES['userImage']['type'] =='image/jpg' || $_FILES['userImage']['type'] =='image/png' && $_FILES['userImage']['size'] < 20000){
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
define('DESIRED_IMAGE_WIDTH', 130);
define('DESIRED_IMAGE_HEIGHT', 130);
$timestamp = time();
$source_path = $_FILES['userImage']['tmp_name'];
$final_location = USER_PROFILE_UPLOAD_PATH.$timestamp.$_FILES['userImage']['name'];
$final_location = strtolower($final_location); //Create image name with lower case
$db_file_name = $timestamp.$_FILES['userImage']['name'];
$db_file_name = strtolower($db_file_name); //Create image name with lower case
/*
* Add file validation code here
*/
list($source_width, $source_height, $source_type) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif($source_path);
break;
case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng($source_path);
break;
}
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;
if ($source_aspect_ratio > $desired_aspect_ratio) {
/*
* Triggered when source image is wider
*/
$temp_height = DESIRED_IMAGE_HEIGHT;
$temp_width = ( int ) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
} else {
/*
* Triggered otherwise (i.e. source image is similar or taller)
*/
$temp_width = DESIRED_IMAGE_WIDTH;
$temp_height = ( int ) (DESIRED_IMAGE_WIDTH / $source_aspect_ratio);
}
/*
* Resize the image into a temporary GD image
*/
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
/*
* Copy cropped region from temporary image into the desired GD image
*/
$x0 = ($temp_width - DESIRED_IMAGE_WIDTH) / 2;
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT) / 2;
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
);
/*
* Render the image
* Alternatively, you can save the image in file-system or database
*/
header('Content-type: image/jpeg');
imagejpeg($desired_gdim, $final_location, 100);
// Retrieve current user profile image
$sql = "SELECT profile_picture FROM ".TABLE_USER_DETAIL." WHERE user_id=84";
$query = $this->db->prepare($sql);
$query->execute();
$result = $query->fetchAll();
$current_picture = $result[0]->profile_picture; // Get admin current profile image
$sql = "UPDATE ".TABLE_USER_DETAIL." SET profile_picture = :profile_picture WHERE user_id=84";
$query = $this->db->prepare($sql);
$updateProfile = $query->execute(array(':profile_picture' => $db_file_name));
if($updateProfile && !empty($current_picture)) {
unlink(USER_PROFILE_UPLOAD_PATH.$current_picture); // Unlink old profile picture
}
?>
<img src="<?php echo URL.$final_location; ?>" />
<?php
}
}
}else if($_FILES['userImage']['name'] == ""){
echo "Error: Please upload your image.";
}else if($_FILES['userImage']['type'] =='image/jpeg' || $_FILES['userImage']['type'] =='image/jpg' || $_FILES['userImage']['type'] =='image/png' && $_FILES['userImage']['size'] > 20000){
echo "Error: File is too large. File must be less than 2MB.";
}else{
echo "Error: Image must be in JPEG, JPG or PNG format.";
}
}
答案 0 :(得分:0)
试试这个:
$.ajax({
url: BASE_URL+"user/updateUserImage",
type : "POST",
data : {new FormData(this) + 'id:'<?php echo $user_id; ?>},
contentType : false,
cache : false,
processData : false,
success : function(data) {
$('#loading-div').hide(); // Hide ajax loader
if(data == "Error: Please upload your image."){
$('#errorBox').html(data);
}else if(data == "Error: File is too large. File must be less than 2MB."){
$('#errorBox').html(data);
}else if(data == "Error: Image must be in JPEG, JPG or PNG format."){
$('#errorBox').html(data);
}else{
$("#change_photo").html(data);
$('.featherlight-close').click();
}
},
error : function() {
}
});
答案 1 :(得分:0)
因为这是ajax中的文件字段所以你不能直接发送它而不是你应该使用ajaxform jquery插件,它可以发送包括文件在内的数据的技巧