fileupload在ajax

时间:2016-04-02 11:43:47

标签: php php-5.6

我有文件上传表单字段,我选择了一个gif&#39;,&#39; png&#39; &#39; JPG&#39;意味着它会工作,我选择任何其他文件.mp3,.php文件,它会给出错误,如**SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 73 of the JSON data**。我想文件类型检查和文件大小< / strong>之后我想插入值,但不知道怎么做,我认为我的PHP代码应该是错误的...

&#13;
&#13;
<?php
 $filename  = basename($_FILES['file']['name']);
 $extension = pathinfo($filename, PATHINFO_EXTENSION);
 $new_name= md5($filename.time()).'.'.$extension;
 if (move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name)) {
	// FILE TYPE CHECKING
	 $allowed =  array('gif','png' ,'jpg');
	 if(!in_array($extension,$allowed) ) {
		$newuser = array('photoname' => $new_name, "message" => "error");
		if($_FILES['file']['size'] > 2459681 ){
			$newuser = array('photoname' => $new_name, "message" => "filesize is to large");
		}else{
			$newuser = array('photoname' => $new_name, "message" => "success");
		}
		echo json_encode($newuser);
	}
	else{
		$newuser = array('photoname' => $new_name, "message" => "success");
	} 
	echo json_encode($newuser);

}else{
	//echo "Error";
	$newuser = array("message" => "file is not moving");
	echo json_encode($newuser);
}	
?>
   <script type="text/javascript">
                  $(document).ready(function(){
                    $("#user-submit").click(function(event){
                      event.preventDefault();
                      if($("form#newUserForm").valid()){
                      var formData = new FormData();
                      var formData = new FormData($('#newUserForm')[0]);
                      formData.append('file', $('input[type=file]')[0].files[0]);
                        $.ajax({
                        url: 'horoscope-check.php',
                        type: 'POST',
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (data) {
                            var res=jQuery.parseJSON(data);// convert the json
                            console.log(res);
                        },
                        
                    });

                      return false;
                      }else{
                        console.log("false");
                      }
                    });
                  });
                </script>
&#13;
<form class="form-horizontal form-bordered" method="POST" id="newUserForm" enctype="multipart/form-data">
          <div class="form-group">
            <label class="col-md-3 control-label">Photo Upload</label>
            <div class="col-md-6">
            <div class="fileupload fileupload-new" data-provides="fileupload">
            <div class="input-append">
            <div class="uneditable-input">
            <i class="fa fa-file fileupload-exists"></i>
            <span class="fileupload-preview"></span>
            </div>
            <span class="btn btn-default btn-file">
            <span class="fileupload-exists">Change</span>
            <span class="fileupload-new">Select file</span>
             <input type="file" id="file" name="file" value="" aria-required="true" required="" data-msg-required="Please select your file">
            </span>

            <a href="#" class="btn btn-default fileupload-exists" data-dismiss="fileupload">Remove</a>
            </div>
            </div>
            </div>
          </div>
           <div class="form-group">
            <div class="col-sm-offset-3 col-sm-6">
              <button class="btn btn-info" type="submit" id="user-submit">Submit</button>
            </div>
          </div>
      </form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用xhr()可以解决您的问题......例如: -

jedis.subscribe