如何使用php中的move_uploaded_file函数上传没有表单的文件

时间:2017-01-25 15:23:19

标签: javascript php ajax

我已经开发了与ajax和php的一对一聊天现在请任何人告诉我如何使用php中的move_uploaded_file函数上传文件。我收到文件变量doc = document.getElementById('doc').value;,但文件没有上传,虚假的路径地址显示

的JavaScript

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type ="text/javascript">
function setfilename(val){
    var fileName = val.substr(val.lastIndexOf("\\")+1, val.length);
    document.getElementById("msg").value = fileName;
    document.getElementById("msg").readOnly = true;
}

function getmsg(){
    document.getElementById("msg").readOnly = false;

    lol = document.getElementById('msg').value;
    doc = document.getElementById('doc').value;

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("suggestion").innerHTML=xmlhttp.responseText;
            document.getElementById('msg').value = '';
            var msgList = document.getElementById("mess");
            msgList.scrollTop = msgList.scrollHeight;
            document.body.scrollTop = document.body.scrollHeight;
        }
    }

    if(lol==''){
        xmlhttp.open("GET","chat",true);
        xmlhttp.send(); 
    }else{
        xmlhttp.open("GET","chat?message="+doc,true);
        xmlhttp.send();
    }
}
</script>

HTML

<div id="chat_box" class="chat_window">
    <div class="top_menu">
        <div class="buttons">
            <div class="button close"></div>
            <div class="button minimize"></div>
            <div class="button maximize"></div>
        </div>
        <div class="title">Messaging about order-id: ODR-46</div>
    </div>

    <div id="your_div">
        <ul class="messages" id="mess" style="overflow:scroll;" >
            <li id="suggestion"></li>
        </ul>
    </div>

    <div class="bottom_wrapper clearfix">
        <div class="message_input_wrapper">
            <input class="message_input" onkeydown="if (event.keyCode == 13) document.getElementById('send').click()" id="msg" placeholder="Type your message here..." />
        </div>
        <a id="send" onclick="return getmsg();">
            <input type="file" onchange="setfilename(this.value);" id="doc">
            <div class="send_message">
            <div class="icon"></div>
            <div class="text" >Send</div>
            </div>
        </a>
    </div>
</div>

<div class="message_template">
    <div class="avatar"></div>
    <div class="text_wrapper">
        <div class="text"></div>
    </div>
</div>

php代码

// move_uploaded_file(get variable from javascript that is doc,'document/'); 

1 个答案:

答案 0 :(得分:-1)

如果你真的想在不使用move_uploaded_file函数的情况下上传文件,请改用它:你可以使用这个

  

http://fineuploader.com/

这会使用分块上传您指定的目录中的文件。