doujiepin5547 2017-03-16 14:42
浏览 68
已采纳

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

I'm building a form to upload images and other data and I want it to return json validation or success response via the ajax success function.

Problem is the php script seems to do some of the if statements, but then completely ignores the extensions validation statement and the file size validation.

I have a switch/case statement in the AJAX to handle the json response from the php script. the switch/case statement then shows the respective message and then fades out.

note: No errors were found in the console.

Thank you for any help.

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 functions(to handle ajax response):

<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 script

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条回答 默认 最新

  • dora0817 2017-03-16 15:39
    关注

    The reason that the images are uploading when, the file size is large and also when the ext is wrong, is simple because, you are allowing your script to upload even if the errors exists, what you need is to have an error counter variable so that when the error occurs you increment the variable then only upload the image when the error counter is zero

    here how :

    <?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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?