【问题标题】:HTML file upload not sending files to $_FILESHTML 文件上传未将文件发送到 $_FILES
【发布时间】:2013-10-25 18:17:34
【问题描述】:

我有一个 html 表单,当用户选择一个文件并提交它时,php 文档中的 $_FILES 没有收到它。主要焦点是表单,当我点击提交时,没有文件被提交,但 onclick 事件会触发。并且没有错误信息

    <form id="artist_post" enctype="multipart/form-data" method="post" onsubmit="return " action="php_parsers/newsfeed_system.php">
<textarea id="statustext" onkeyup="statusMax(this,250)"></textarea>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input id="audioUpload" type="file" name="audioUpload" accept="audio/*" />
<input type="submit" value="Post" id="statusBtn" onclick="postToStatus('status_post','a','statustext')"/>
</form>

这是 php 文档,我只是想看看文件是否正在上传

    if (isset($_FILES["audioUpload"]["name"])) {
        echo "flag1";
    }
    if ($_FILES["audioUpload"]["tmp_name"] != "") {
        echo "flag2";
    }

javascript从表单中获取信息并通过ajax发送给php。

function postToStatus(action,type,ta){
var data = _(ta).value;
if(data == ""){
    alert("Type something first");
    return false;
}

// Checks to see if user uploaded a file to send with post 
if(_("audioUpload").value != "") { 
    type = "c";
}   

_("statusBtn").disabled = true;
var ajax = ajaxObj("POST", "php_parsers/newsfeed_system.php");
ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {
        //Pull some info from whatever the php doc returns
    }
}

ajax.send("action="+action+"&type="+type+"&data="+data);
}
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();  // create new http request 
x.open( meth, url, true );  // open request and pass it a method and a url to post it to 
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
return x;
}
// Modular function that externalizes readyState and status check
function ajaxReturn(x) {
if(x.readyState == 4 && x.status == 200) {
    return true;
    }
}

【问题讨论】:

  • 你的javascriptpostToStatus()函数包含什么?
  • 首先取出onsubmit="return "onclick="postToStatus()",除非你正确调用那个/那些函数。
  • 您是否尝试通过 javascript 发布文件?因为没有 jquery lib 就无法工作
  • @JerkoW.Tisler “没有 jquery 库”;你知道 jquery 是用 in javascript 编写的,而且你可以用 jquery 做的所有事情都可以,因此,不用它(在原始 javascript 中)?
  • 是的,你可以在 JS 中做到这一点。我的意思是,你是对的 JQuery 是用 javascript 编写的,但老实说,我真的怀疑你写了 1000 多行代码来用原生 JS 上传文件

标签: php forms file-upload


【解决方案1】:

您的&lt;form action="..."&gt; 需要删除。您正在使用 AJAX,因此一起绕过了表单操作序列。我已经在我的服务器上创建了这个副本,并且没有那个副本。

我建议不时将其放入 JSFiddle 并点击顶部的“Tidy”按钮。否则,到目前为止,代码做得很好。

如果您想查看我的复制副本:click here

【讨论】:

  • 当我取出 '
    ' 它仍然没有将文件发送到 php 文档。我同意并感谢您的意见,我认为由于我使用 ajax 绕过而无法正常工作。但是我似乎无法弄清楚为什么文件不会发送。 Javascript 无法读取文件,这就是我最初对其进行编码的方式。还有其他建议吗?
猜你喜欢
  • 2011-04-23
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2015-10-11
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多