0

i'm following this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/comment-page-1/#comments to learn how to upload multiple files via ajax.

This is my html:

<form class="form-horizontal" id="settingsChangeAvatar" method="post" enctype="multipart/form-data" action="<?php echo $AJAX."/ajaxUpload.php"?>">
    <input class="input-xlarge input-file" id="settingsUploadAvatar" name="settingsUploadAvatar" type="file" multiple />
    <button class="btn" id="uploadAvatarButton" type="submit">Upload</button>
</form>

And this is my ajaxUpload.php:

foreach($_FILES["settingsUploadAvatar"]["error"] as $key => $error){
    if($error == UPLOAD_ERR_OK) {
        $name = $_FILES["settingsUploadAvatar"]["name"][$key];
        move_uploaded_file($_FILES["settingsUploadAvatar"]["tmp_name"][$key], $_SERVER["DOCUMENT_ROOT"]."/webname/".$_FILES["settingsUploadAvatar"]["name"][$key]);
    }
}

echo("File uploaded");

My code should be the same as the one in the tutorial. Thanks for helping.

3
  • Did you simply var_dump($_FILES) to see what's in there? Basic debugging is in order here... Commented Feb 23, 2012 at 14:22
  • 2
    First thing I notice is you're missing [] on the end of the input's name attr: name='settingsUploadAvatar[] Commented Feb 23, 2012 at 14:23
  • 1
    whoever gave this post a -1 clearly has no interest in actually being helpful. the question is clearly defined, take a peek at the title: "PHP multiple file upload: Invalid argument supplied for foreach()". +1. :) Commented Feb 23, 2012 at 14:43

1 Answer 1

6

Change the 'name' attribute of your input from settingsUploadAvatar to settingsUploadAvatar[].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.