【问题标题】:File upload in PHP, ajax shows errorPHP中的文件上传,ajax显示错误
【发布时间】:2016-04-13 14:31:48
【问题描述】:

我正在尝试上传图片文件。

这是我的 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>

我的 JavaScript 代码

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);

我的 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');

当我运行这个时,变量 a-d 得到了很好的处理,但文件没有上传,但显示以下错误

“注意:未定义索引:[object FormData]”

我该如何解决这个问题?

【问题讨论】:

  • 执行print_r($_POST)print_r($_FILES) 并确保$_POST['fileup']$_FILES 中的一个键
  • 看来 fileup 不是$_FILES 中的键,而是$_POST 中的键。此外,它似乎不包含图像数据,只是图像的假路径

标签: javascript php jquery ajax file-upload


【解决方案1】:

首先告诉您的 FORM 它将包含一个上传的对象(或更多)。

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

在这里阅读更多:

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

【讨论】:

  • 我添加了enctype="multipart/form-data"。我想通过 ajax 传递它,以便根据从 php 返回的结果,我可以在页面中进行一些修改。因此我没有添加动作属性
【解决方案2】:

问题就在这里:

ajax.send("a="+a+"&b="+b+"&c="+c+"&d="+d+"&fileup="+fileup);

您正在尝试将 FormData 对象附加到字符串。相反,您应该将所有其他值附加到 FormData 对象并发送。

var fileup = new FormData();                  
fileup.append("file", file_data);
fileup.append("a", a);
fileup.append("b", b);
fileup.append("c", c);
fileup.append("d", d);

ajax.send(fileup);

【讨论】:

  • 谢谢,我在 php 中尝试了$_POST["a"],但它不起作用。它说未定义索引:a。如何获取 php 中的值
  • 我做了一个print_r($_POST),结果出现了Array ( [------WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition:_form-data;_name] =&gt; "a" atext ------WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition: form-data; name="b" btext ------WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition: form-data; name="c" ctext ------WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition: form-data; name="d" dtext ------WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition: form-data; name="file"; filename="file.png" Content-Type: image/png �PNG IHDReFZ )
  • 在发送调用之前的 JavaScript 中,尝试 ajax.setRequestHeader('Content-type', 'multipart/form-data'); 然后再次查看 PHP 帖子中的内容。
  • 还是一样
  • 我想我会使用XMLHttpRequest而不是通过ajax
猜你喜欢
  • 2013-12-04
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
相关资源
最近更新 更多