php - FormData sends files but $_FILE is empty -
i have no idea why keep getting empty $_files global array when print out. ive been looking on can't see going wrong. because of have shown of code. if use button reaches server fine , works out. if use drag , drop no files on server? idea why? when print array array(0).
i found 1 problem while loop fixed still no files tho. incase people wondering if post action correct. url rewritten using mod_rewrite. displays correct php pages assuming the pages reached. mod_rewrite affect uploading of files. 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 file here…</legend> <p>or click button <em>browse</em>…</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 server file. doesn't matter incase
<?php echo "server reached\n"; var_dump($_post); var_dump($_files); exit(); ?>
try without rewrite
, issue.
Comments
Post a Comment