简体   繁体   English

上载多个文件不起作用

[英]Uploading multiple files doesn't work

So I'm trying to circumvent the max_post_size in PHP (since I don't have access to the server's php.ini) by uploading the files sending each file to a XMLHttpRequest. 因此,我试图通过将发送每个文件的文件上传到XMLHttpRequest来规避PHP中的max_post_size(因为我无权访问服务器的php.ini)。 I'm prety new to PHP/JS so I might've missed something really obvious. 我是PHP / JS的新手,所以我可能错过了一些非常明显的东西。

The problems I have is that files don't get uploaded and the responseText always prints "Hello World!"... 我的问题是文件没有被上传,并且responseText总是打印“ Hello World!”。

Right now this is my code: 现在,这是我的代码:

<?php
if (isset($_FILES['myFile'])) {
// Example:
    move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);
    echo "responseText???";
    exit;
}?>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<input type="file" multiple="multiple" id="filesInput" /> <br/>
<input type="button" id="uploadButton" value="Buton" />
<script type="text/javascript">

function sendFile(file) {
    var uri = "/index.php";
    var xhr = new XMLHttpRequest();
    var fd = new FormData();

    xhr.open("POST", uri, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            // Handle response.
            //alert(xhr.responseText); // This only prints Hello World...:/
        }
    };
    fd.append('myFile', file);
    // Initiate a multipart/form-data upload
    xhr.send(fd);
}

$("#uploadButton").on("click", function(){
    var files = $('#filesInput').prop('files');
    var len = files.length;
    for(var i=0; i<len; i++){
        $("#uploadFileNames").append(files[i].name + "<br/>");
        sendFile(files[i]);
    }
});

</script>

PS: Part of the code was taken from Mozilla website: PS:部分代码来自Mozilla网站:

https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Example:_Uploading_a_user-selected_file https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications#Example:_Uploading_a_user-selected_file

Well, this is stupid by me. 好吧,这是我的愚蠢。 I had this variable set to : 我将此变量设置为:

var uri = "/index.php"; 

But my upload.php file was in: /home/web_stuff/upload/index.php 但是我的upload.php文件位于:/home/web_stuff/upload/index.php

So it always returned the text from: /home/index.php and looked for the upload form there too 因此,它总是从以下位置返回文本:/home/index.php,并在那里也查找上传表单

which I set to "Hello, World!" 我将其设置为“你好,世界!” (this confused me the most, why would it always return Hello World). (这让我最困惑,为什么总是返回Hello World)。

Conclusion: The problem was with the path to the php file. 结论:问题出在php文件的路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM