2

I have no idea why i keep getting an empty $_FILES global array when i print it out. Ive been looking it over but i can't see where i am going wrong. because of that i have shown all of the code. if i use the button it reaches the server just fine and everything works out. but if i use the drag and drop then i get no files on the server? any idea why? when i print the array it is array(0).

I found one problem with the while loop which i now fixed still no files tho. just incase people are wondering if the post action is correct. The url is rewritten using mod_rewrite. It displays the correct php pages so i am assuming the the pages is reached. would mod_rewrite affect the uploading of files. I don't think so...?

    <!DOCTYPE html>
<html>
      <head>
            <title>Music Upload</title>
            <style>
                  #zone
                  {
                        height:300px;
                        width:500px;
                        border:1px solid black;
                  }
            </style>
      </head>
      <body>
            <div id="zone">
            <legend>Drop a file here&hellip;</legend>
            <p>Or click button to <em>Browse</em>&hellip;</p>
            </div>
            <form action="../receive/" method="POST" enctype="multipart/form-data">
                <input type="file" name="file[]" multiple />
                <input type="submit" value="submit" />
            </form>


            <script>
                  function dragover(event)
                  {
                        event.preventDefault();
                        console.log("drag event");
                        return false;
                  };
                  function dragend(event)
                  {
                        event.preventDefault();
                        console.log("drag end event");
                        return false;
                  };
                  function drop(event)
                  {
                        console.log("Files droped");
                        event.preventDefault();
                        var files=event.dataTransfer.files;
                        fileupload(files);

                  };

                  function isMp3(file)
                  {
                        console.log("Check if mp3 file");
                        mimeTypes=['audio/mpeg','audio/mp3','audio/x-mpeg-3'];
                        for(var i=0;i<mimeTypes.length;i++)
                        {
                              if(file.type==mimeTypes[i])
                              {
                                    return true;
                              };
                        };
                        return false;
                  };

                  function fileupload(files)
                  {
                        if(files.length>0)
                        {

                              var formData= new FormData();
                              var i=0;
                              while(i<files.length)
                              {
                                    var file= files.item(i);

                                    if(isMp3(file)){
                                          formData.append('file[]',files[i++]);
                                          console.log('valid mp3');
                                    }
                              }
                              var xhr = new XMLHttpRequest();
                              xhr.open('POST','/mymusic/receive',true);
                              xhr.onload=function()
                              {
                                    if(this.status==200)
                                    {
                                          console.log('data sent');
                                          console.log(this.responseText);

                                    }else
                                    {
                                          console.log('data failed');
                                    }
                              };
                              xhr.upload.onprogress=function(event)
                              {
                                    if(event.lengthComputable)
                                    {
                                          var complete=Math.round(event.loaded*100/event.total);
                                          console.log(complete+"%");
                                    }
                              };
                              xhr.send(formData);
                        }
                  };

                  var dropArea=document.getElementById("zone");
                  dropArea.addEventListener("dragover",dragover,false);
                  dropArea.addEventListener("dragend",dragend,false);
                  dropArea.addEventListener("drop",drop,false);
                  console.log('script loaded');
            </script>
      </body>
</html>
<?php exit();?>

Here is the server file. it doesn't really matter but just incase

<?php

echo "server reached\n";
  var_dump($_POST);
  var_dump($_FILES);
  exit();
?>
Lpc_dark
  • 2,834
  • 7
  • 32
  • 49
  • if you change the name to file_up instead of file[] ?? – Hackerman May 14 '13 at 14:55
  • i don't see why that would affect it. I have file[] because it's an array of files that will be coming in and they have the same name. not the name of the files but the name value for the input – Lpc_dark May 14 '13 at 14:56
  • Possible duplicate of [Upload File With Ajax XmlHttpRequest](http://stackoverflow.com/questions/6211145/upload-file-with-ajax-xmlhttprequest) worded differently but the answer should be the same – Anigel May 14 '13 at 14:58
  • nope not a duplicate he was sending the file and not the formdata object it self. I am sending the formdata – Lpc_dark May 14 '13 at 14:59
  • ps url are rewritten is the reason i don't have extensions on the file. I know that will come up so i answered – Lpc_dark May 14 '13 at 15:00
  • Just for testing....with just one file and see if the upload is working...if not then the problem maybe can be your Javascript code – Hackerman May 14 '13 at 15:00
  • i tested it with one file and it says that it's uploading in the progress in the console.log but when i get to the server it's empty – Lpc_dark May 14 '13 at 15:01
  • The problem is with the javascript as it works fine if they use the form. I cannot see where OP is setting the multipart/form-data in the JS but as I am no JS expert I will bow out – Anigel May 14 '13 at 15:02
  • @Anigel you shouldn't sent the content type when using FormData as the browser must set that itself to include the boundary name. – idbehold May 14 '13 at 15:06
  • Fair enough I just saw it was set in some previous answers, and know that in the case of normal forms, files will always be missing on the server without it. – Anigel May 14 '13 at 15:07
  • Why is your form action set to `../receive/`? Is it called `index.php`? – Funk Forty Niner May 14 '13 at 15:11
  • url rewritten. it reaches the pages because it prints the array. it's just empty – Lpc_dark May 14 '13 at 15:13
  • @Lpc_dark have you tried/tested it without it being rewritten? – Funk Forty Niner May 14 '13 at 15:20
  • no but i really don't think that would matter because the file upload works when it's not the drag and drop and i use the button. If the rewritten was the problem wouldn't that fail as well? – Lpc_dark May 14 '13 at 15:22
  • 1
    @Lpc_dark I don't know if it would fail. Try it and see. It's part of "Troubleshooting" ;-) – Funk Forty Niner May 14 '13 at 15:23
  • sigh k but i really doubt it. ill give it a shot – Lpc_dark May 14 '13 at 15:24
  • blasphemy now it works. that's some serious bull. hmm maybe url rewritten can't work while uploading a file. doesn't really make any sense to me tho can anyone explain? why does the standard form work but the formdata doesn't? – Lpc_dark May 14 '13 at 15:30
  • @Lpc_dark So it works without the rewrite, yes? As for explaining it, I can't answer that. – Funk Forty Niner May 14 '13 at 15:32
  • ye it did makes no sense to me. if you want to post the answer i will give a nice checkmark i know you can't really explain why tho but it's okay. – Lpc_dark May 14 '13 at 15:34
  • 1
    @Lpc_dark Indeed, most bizarre. Answer posted. Glad it works for you though. Cheers – Funk Forty Niner May 14 '13 at 15:37

1 Answers1

4

Try it without the rewrite, that is most probably the issue.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141