douju5062 2016-04-13 14:31
浏览 64

用PHP上传文件,ajax显示错误

I am trying to upload an image file.

This is my Html

    <form class="form-horizontal" role="form" enctype="multipart/form-data">
    <div class="form-group">
        <label for="a" class="control-label col-sm-2">A:</label>
        <input type="text" class="form-control col-sm-10" id="a">
    </div>
     <div class="form-group">
        <label for="b" class="control-label col-sm-2">B:</label>
        <input type="text" class="form-control col-sm-10" id="b">
    </div>
    <div class="form-group">
        <label for="c" class="control-label col-sm-2">C:</label>
        <input type="text" class="form-control col-sm-10" id="c">
    </div>
    <div class="form-group">
        <label for="d" class="control-label col-sm-2">D:</label>
        <input type="text" class="form-control col-sm-10" id="d">
    </div>   
    <div class="form-group">
        <label for="fupload" class="control-label col-sm-2">Upload image:</label>
        <input type="file" class="form-control col-sm-10" id="fupload">
    </div>   
    <button type="button" class="btn-lg btn-primary" style="margin-left:200px" id="new_save" onclick='save_all();'>Save</button>
</form>

My javascript code

var a= _("a").value; //the _ function returns document.getElementById(x)
var b = _("b").value;
var c = _("c").value;
var d = _("d").value;

var file_data = $("#fupload").prop("files")[0];  
var fileup = new FormData();                  
fileup.append("file", file_data)

var ajax = ajaxObj("POST", "./phps/saveall.php");
ajax.onreadystatechange = function() { 
        alert(ajax.responseText);
    }
ajax.send("a="+a+"&b="+b+"&c="+c+"&d="+d+"&fileup="+fileup);

Finally my PHP

   $a = preg_replace('#[^a-z0-9()., ]#i', '', $_POST['a']);
   $b = preg_replace('#[^a-z0-9()., ]#i', '', $_POST['b']);
   $c= htmlentities($_POST['c']);
   $c= mysqli_real_escape_string($db_conx, $c);
   $d = htmlentities($_POST['d']);
   $d = mysqli_real_escape_string($db_conx, $d);
   $fup =  $_POST['fileup'];
   //processing a-d
   //this is where the problem comes
   move_uploaded_file($_FILES[$fup]['tmp_name'], '../lyrics/'.$a.'.png');

When I run this the variables a-d gets processed fine but file does not get uploaded but shows the following error

"Notice: Undefined index: [object FormData]"

How could I fix this?

  • 写回答

2条回答 默认 最新

  • dongzou9047 2016-04-13 14:38
    关注

    Start with telling your FORM that it will contain an uploaded object (or more).

    <form enctype="multipart/form-data" action="...put your URL here..." method="POST">
    

    Read more here:

    http://php.net/manual/en/features.file-upload.post-method.php

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c