简体   繁体   中英

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. I'm prety new to PHP/JS so I might've missed something really obvious.

The problems I have is that files don't get uploaded and the responseText always prints "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:

https://developer.mozilla.org/en-US/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

So it always returned the text from: /home/index.php and looked for the upload form there too

which I set to "Hello, World!" (this confused me the most, why would it always return Hello World).

Conclusion: The problem was with the path to the php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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