来自php脚本的Json响应不起作用

时间:2017-03-16 14:42:09

标签: php jquery json ajax

我正在构建一个表单来上传图像和其他数据,我希望它通过ajax成功函数返回json验证或成功响应。

问题是php脚本似乎做了一些if语句,但后来完全忽略了扩展验证语句和文件大小验证。

我在AJAX中有一个switch / case语句来处理来自php脚本的json响应。然后switch / case语句显示相应的消息然后淡出。

注意:控制台中未发现任何错误。

感谢您的帮助。

AJAX:

$(document).ready(function (e) {
    $('.add_business_form').on('submit',(function(e) {  

        e.preventDefault();

        var formData = new FormData(this);


        $.ajax({
            type:'POST',
            url: $(this).attr('action'),
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            dataType: "json",
            success:function(response){

    switch(response.message){
      case 'logo_success': 
         logoSuccess();
      break;
      case 'file_is_not_image': 
        file_is_not_image();
      break;
      case 'No_file_selected': 
        No_file_selected();
      break;        

      case 'wrong_logo_extention':
        Wrong_extention();
      break;        
       case 'logo_too_big':
        logo_too_big();
      break;
       case 'unknown_error':
        unknown_error();
      break;
    }           

            },
            error: function(data){   


            }
        });
    }));

}); 

jQuery函数(处理ajax响应):

<script>
 function logoSuccess(){
 $('.response_success').fadeIn('fast').delay(10000).fadeOut('fast');    
$('.add_business_form')[0].reset();  
 }      
</script>   
<script>
 function file_is_not_image(){
 $('.file_is_not_image').fadeIn('fast').delay(10000).fadeOut('fast');    
 }      
</script>
<script>
 function No_file_selected(){
 $('.No_file_selected').fadeIn('fast').delay(10000).fadeOut('fast');         
 }      
</script>
<script>
 function logo_too_big(){
 $('.logo_too_big').fadeIn('fast').delay(10000).fadeOut('fast');         
 }      
</script>
<script>
 function unknown_error(){
 $('.unknown_error').fadeIn('fast').delay(10000).fadeOut('fast');        
 }      
</script>

PHP脚本

if(empty($_FILES['uploaded_img_preview']['name'])){
    $response["message"] = 'No_file_selected';
}else{

$imgFile = $_FILES['uploaded_img_preview']['name'];
$tmp_dir = $_FILES['uploaded_img_preview']['tmp_name'];
$imgSize = $_FILES['uploaded_img_preview']['size'];
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$upload_dir = '../images/'; // upload directory


   // valid image extensions
   $valid_extensions = array('jpg','png','jpeg'); // valid extensions

   // rename uploading image
   $new_logo_name = rand(1000,1000000).".".$imgExt;

   // allow valid image file formats
   if(in_array($imgExt, $valid_extensions)){   
    // Check file size '5MB'
    if($imgSize < 500000)    {

    }
    else{
     $response["message"] = 'logo_too_big';
    }
   }
   else{
     $response["message"] = 'wrong_logo_extention';  
   }

    if (move_uploaded_file($tmp_dir,$upload_dir.$new_logo_name)){
     $response["message"] = 'logo_success'; 
    }else{
    $response["message"] = 'unknown_error'; 

    }
 }

echo json_encode($response);
exit();

1 个答案:

答案 0 :(得分:1)

图像上传的原因是,文件大小以及ext错误时,很简单,因为即使存在错误,您也允许上传脚本,所需要的是出错计数器变量,以便在发生错误时增加变量,然后仅在错误计数器为零时上传图像

这里如何:

<?php

$errors = "";//Count error
if (empty($_FILES['uploaded_img_preview']['name'])) {
    $response["message"] = 'No_file_selected';
} else {

    $imgFile    = $_FILES['uploaded_img_preview']['name'];
    $tmp_dir    = $_FILES['uploaded_img_preview']['tmp_name'];
    $imgSize    = $_FILES['uploaded_img_preview']['size'];
    $imgExt     = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION));
    $upload_dir = '../images/'; // upload directory


    if ($imgExt != "jpg" && $imgExt != "png" && $imgExt != "jpeg" && $imgExt != "gif") {
        $response["message"] = 'wrong_logo_extention';

        $errors++; //increment
    }

    if ($imgSize > 500000) {

        $response["message"] = 'logo_too_big';
        $errors++; //increment
    }
    // rename uploading image
    $new_logo_name = rand(1000, 1000000) . "." . $imgExt;

    //upload only if there are no errors
    if ($errors <= 0) {

        if (move_uploaded_file($tmp_dir, $upload_dir . $new_logo_name)) {
            $response["message"] = 'logo_success';
        } else {
            $response["message"] = 'unknown_error';

        }

    }


}

echo json_encode($response);
exit();
?>