我的代码使用ajax php上传图片有什么问题?

时间:2015-12-28 09:49:03

标签: javascript php jquery ajax

我有一个配置文件设置页面,用户可以在其中更改其个人资料信息,图片等。编写更改信息的代码并不难,但对于图像更改我使用的是我在网站上找到的ajax php插件。现在这个插件有两个页面,一个用于将图像存储到数据库中的php代码和一个用于动态上载图像的表单的ajax页面。 ajax代码和表单在我的setting.php页面上,它还有一些其他的javascript和php验证。现在在这个页面javascript验证的配置文件信息是我的麻烦,当我尝试更改图像时没有上传图像,但当我删除javascript验证代码图像上传插件工作正常。有人可以解释这里有什么问题以及如何解决它?

的settings.php

<script>
$(document).on('change', '#image_upload_file', function () {
var progressBar = $('.progressBar'), bar = $('.progressBar .bar'), percent = $('.progressBar .percent');

$('#image_upload_form').ajaxForm({
beforeSend: function() {
    progressBar.fadeIn();
    var percentVal = '0%';
    bar.width(percentVal)
    percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
    var percentVal = percentComplete + '%';
    bar.width(percentVal)
    percent.html(percentVal);
},
success: function(html, statusText, xhr, $form) {       
    obj = $.parseJSON(html);    
    if(obj.status){     
        var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);
        $("#imgArea>img").prop('src',obj.image_medium);         
    }else{
        alert(obj.error);
    }
},
complete: function(xhr) {
    progressBar.fadeOut();          
}   
}).submit();        

});
</script>

验证设置页面上的个人资料信息的其中一个javascript代码

<script type="text/javascript">
    $(document).ready(function() {
    $('#emailup').click(function() {
      var email = document.getElementById('email').value;
      var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;      
            if (!filter.test(email) || email == "") {
                $('#err').html('Enter a valid email');
                return false;
            }
            else if (email.length > 255) {
                $('#err').html('Enter a valid email');
                return false;
            }
           });
        });
  </script>

phpcode.php

<?php
if(isset($_FILES['image_upload_file'])){
$output['status']=FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif",   "image/jpeg",   "image/pjpeg",   "image/png",   "image/x-png"  );

if ($_FILES['image_upload_file']["error"] > 0) {
    $output['error']= "Error in File";
}
elseif (!in_array($_FILES['image_upload_file']["type"], $allowedImageType)) {
    $output['error']= "You can only upload JPG, PNG and GIF file";
}
elseif (round($_FILES['image_upload_file']["size"] / 1024) > 4096) {
    $output['error']= "You can upload file size up to 4 MB";
} else {
    /*create directory with 777 permission if not exist - start*/
    createDir(IMAGE_SMALL_DIR);
    createDir(IMAGE_MEDIUM_DIR);
    /*create directory with 777 permission if not exist - end*/
    $path[0] = $_FILES['image_upload_file']['tmp_name'];
    $file = pathinfo($_FILES['image_upload_file']['name']);
    $fileType = $file["extension"];
    $desiredExt='jpg';
    $fileNameNew = rand(333, 999) . time() . ".$desiredExt";
    $path[1] = IMAGE_MEDIUM_DIR . $fileNameNew;
    $path[2] = IMAGE_SMALL_DIR . $fileNameNew;

    if (createThumb($path[0], $path[1], $fileType, IMAGE_MEDIUM_SIZE, IMAGE_MEDIUM_SIZE,IMAGE_MEDIUM_SIZE)) {

        if (createThumb($path[1], $path[2],"$desiredExt", IMAGE_SMALL_SIZE, IMAGE_SMALL_SIZE,IMAGE_SMALL_SIZE)) {
            $output['status']=TRUE;
            $output['image_medium']= $path[1];
            $output['image_small']= $path[2];
        }
    }
}
echo json_encode($output);
}
?>

0 个答案:

没有答案