简体   繁体   English

使用PHP验证进行AJAX文件上载

[英]AJAX File Upload with PHP Validation

I have a form that has two text fields and a file upload field. 我有一个表单有两个文本字段和一个文件上传字段。

The validation is handled completely from PHP and I'm using a little bit of Ajax to retrieve the error messages the PHP script produces via an array (err[]). 验证完全由PHP处理,我使用一点Ajax来检索PHP脚本通过数组生成的错误消息(err [])。

My problem is, I can't seem to get the file upload validation to work correctly. 我的问题是,我似乎无法让文件上传验证正常工作。 (when uploading a file, it will always say "Wrong file format only .png , .gif, .jpg, .jpeg are accepted") (上传文件时,总是会说“错误的文件格式只接受.png,.gif,.jpg,.jpeg”)

The Ajax is below: Ajax如下:

function checkform() {
        $.post('upload.php', $("form#uploadForm").serialize(), function (data) {

                $("div#error").html(data).slideDown("fast");

                var destination = $('div#uploadContainer').offset().top - 15;
                $("html:not(:animated),body:not(:animated)").animate({
                    scrollTop: destination
                }, 200);

            });
    return false;
}

The following validation seems to ALWAYS be triggered: 以下验证似乎总是被触发:

$extension = strrchr($_FILES['uploadFile']['name'], '.');
    if (!in_array($extension, $extensions)) {
        $err[]='Wrong file format only .png , .gif, .jpg, .jpeg are accepted';
    }
...
...
if (isset($_FILES['uploadFile']) && $_FILES['uploadFile']['size'] != 0)
{
    // Upload file
}
$extension = end(explode('.', $_FILES['uploadFile']['name']));

if (!in_array($extension, $extensions))
{
    $err[]='Wrong file format only .png , .gif, .jpg, .jpeg are accepted';
}

As $extension has extension without dot(.). 因为$ extension有扩展而没有点(。)。 Remove dot(.) from file extensions 从文件扩展名中删除点(。)

$extensions = array('.png', '.gif', '.jpg', '.jpeg','.PNG', '.GIF', '.JPG', '.JPEG');

which will be 这将是

$extensions = array('png', 'gif', 'jpg', 'jpeg','PNG', 'GIF', 'JPG', 'JPEG');

Hope this fixes the bug 希望这可以解决这个问题

Per @riddell's comment ( AJAX File Upload with PHP Validation ) Per @ riddell的评论( 使用PHP验证进行AJAX文件上传

the problem is most likely NOT php but actually the Jquery itself. 问题很可能不是php,而是Jquery本身。 something like this could work 像这样的东西可以工作

var form = $('form')[0];
var data = new FormData(form);

    $.ajax({
      url: 'upload.php',
      data: data ,
      processData: false,
      contentType: false,
      type: 'POST',
      success: function(data){
        alert(data);
      }
    });

Correct logic for upload and checking file extension 正确的上传和检查文件扩展名的逻辑

if (isset($_FILES['uploadFile']) && $_FILES['uploadFile']['size'] != 0)
{
    $extension = end(explode('.', $_FILES['uploadFile']['name']));

    if (!in_array($extension, $extensions))
    {
        $err[]='Wrong file format only .png , .gif, .jpg, .jpeg are accepted';
    }

    // Write code to upload image here
}
else
{
    // There was error while uploading image
    // $_FILES['uploadFile']['error'] gives error code
    //  
    // Possible errors
    // UPLOAD_ERR_OK: 0
    // UPLOAD_ERR_INI_SIZE: 1
    // UPLOAD_ERR_FORM_SIZE: 2
    // UPLOAD_ERR_NO_TMP_DIR: 6
    // UPLOAD_ERR_CANT_WRITE: 7
    // UPLOAD_ERR_EXTENSION: 8
    // UPLOAD_ERR_PARTIAL: 3
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM