dqch34769 2018-11-19 08:56
浏览 49

使用jj发送图像时使用jQuery错误上传图片的人员

I have an error when I try to upload an image using jQuery and ajax.

Here is my html:

<label for="img_add_galery">
                    <a class="add">
                        <img click="image_galery" src="../../img/ui/black.png" alt="images">
                    </a>
                  </label>
                </div>
                <input type="file" id="img_add_galery" name="img_add_galery" style="display:none;"/>

This is my js:

$(document).on('change','#img_add_galery',function(){
    var property = document.getElementById('img_add_galery').files[0];
    var image_name = property.name;
    var image_extension = image_name.split('.').pop().toLowerCase();
    if(jQuery.inArray(image_extension, ['gif','png','jpg','jpeg']) == -1)
    {
      alert('invalid image type');
    }
    var image_size = property.size;
    if(image_size > 2000000000000)
    {
      alert('image is to big');
    }
    else
    {
      console.log(property);
        var form_data = new FormData();
        form_data.append("file", property);
        $.ajax({
          url: "traitement/add_image_galery.php",
          method: "POST",
          data: form_data,
          contentType:false,
          cache:false,
          processData:false,
          success:function(data)
          {
            $(".tz-gallery").children().append(data);
        }
      })
  }
})

and my php

var_dump($_FILES["img_add_galery"]["name"]);

if($_FILES["file_img"]["name"] != '')

{

  $test = explode(".",$_FILES["file_img"]["name"]);

  $extension = end($test);

  $name = rand(1,9999) . '.' . $extension;

  $location = '../../../img/galerie/'.$name;

  move_uploaded_file($_FILES["file_img"]["tmp_name"],$location);

  echo '<div class="col-sm-6 col-md-4">
      <a class="delete" img="'.$location.'">
          <img click="image_galery" src="'.$location.'" alt="images">
      </a>
  </div>';
}
  • 写回答

1条回答 默认 最新

  • donglin1692 2018-11-19 16:02
    关注

    When you append the file to your FormData object you use file as its name but in PHP you use file_img, both must match.

    $_FILES["file"]["name"]
    

    In fact, I think you should use the same name everywhere just to avoid confusion

    <input type="file" id="img_add_galery" name="img_add_galery" style="display:none;"/>
    
    form_data.append("img_add_galery", property);
    
    $_FILES["img_add_galery"]["name"]
    

    Also if you want to check what's going on in the file array you should dump the whole thing var_dump($_FILES);

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上