Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <!-- index.html -->
  2. <html>
  3. <head>
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  5. </head>
  6. <body>
  7. <form id="myform" onsubmit="return uploadFiles(this);">
  8. <input type="text" name="desc" />
  9. <input type="file" name="somefile" multiple="1" />
  10. <input type="submit" value="Go" />
  11. </form>
  12. <div id="status">Upload file status</div>
  13. </body>
  14. <script>
  15. // Function to upload a form via FormData, breaking out files and cutting
  16. // any non-named elements. Assumes that there's a #status DIV and the
  17. // URL is hardcoded.
  18. function uploadFiles(frm) {
  19. var formdata = new FormData();
  20. // I'm doing this to separate out the upload content. Note that multiple
  21. // files can be uploaded and will appear as a decently-friendly PHP array
  22. // in $_FILES.
  23. $(frm).find(":input").each(function(idx, ele) {
  24. // This is a file field. Break it out.
  25. if(ele.files) {
  26. for(i=0; i<ele.files.length; i++) {
  27. formdata.append(ele.name + "[" + i + "]", ele.files[i]);
  28. }
  29. // Not a file element, so put in the upload iff there's a name.
  30. } else if(ele.name) {
  31. formdata.append(ele.name, ele.value);
  32. }
  33. });
  34. // Run the AJAX.
  35. $.ajax({
  36. url: "test.php", // Change Me :-)
  37. type: "POST",
  38. data: formdata,
  39. processData: false,
  40. contentType: false,
  41. success: function(data) {
  42. $("#status").html(data);
  43. },
  44. error: function(xhr, status, error) {
  45. $("#status").html("Error uploading file(s): " + error);
  46. },
  47. });
  48. return false;
  49. }
  50. </script>
  51. </html>
  52.  
  53. <!-- test.php -->
  54. <pre>
  55. _FILES Content:
  56. <?php print_r($_FILES) ?>
  57. _POST Content:
  58. <?php print_r($_POST) ?>
  59. </pre>
  60.  
  61. <!-- sample output -->
  62. _FILES Content:
  63. Array
  64. (
  65. [somefile] => Array
  66. (
  67. [name] => Array
  68. (
  69. [0] => 210.jpg
  70. [1] => astoria-desat.jpg
  71. [2] => build-desat.jpg
  72. [3] => control-panel.jpg
  73. [4] => cuda.png
  74. [5] => departure.png
  75. [6] => dietary.png
  76. [7] => embarassment.png
  77. [8] => error.png
  78. [9] => europe.jpg
  79. [10] => europe.png
  80. )
  81.  
  82. [type] => Array
  83. (
  84. [0] => image/jpeg
  85. [1] => image/jpeg
  86. [2] => image/jpeg
  87. [3] => image/jpeg
  88. [4] => image/png
  89. [5] => image/png
  90. [6] => image/png
  91. [7] => image/png
  92. [8] => image/png
  93. [9] => image/jpeg
  94. [10] => image/png
  95. )
  96.  
  97. [tmp_name] => Array
  98. (
  99. [0] => /tmp/phpRPsOcp
  100. [1] => /tmp/phpY0SrcE
  101. [2] => /tmp/phpGOS8bT
  102. [3] => /tmp/phpvi4Yb8
  103. [4] => /tmp/phpvsjlcn
  104. [5] => /tmp/phpEVuKcC
  105. [6] => /tmp/phpCQzbdR
  106. [7] => /tmp/phpouEHd6
  107. [8] => /tmp/phpK16gel
  108. [9] => /tmp/phpvejCfA
  109. [10] => /tmp/php9KJdhP
  110. )
  111.  
  112. [error] => Array
  113. (
  114. [0] => 0
  115. [1] => 0
  116. [2] => 0
  117. [3] => 0
  118. [4] => 0
  119. [5] => 0
  120. [6] => 0
  121. [7] => 0
  122. [8] => 0
  123. [9] => 0
  124. [10] => 0
  125. )
  126.  
  127. [size] => Array
  128. (
  129. [0] => 22650
  130. [1] => 40447
  131. [2] => 21591
  132. [3] => 45202
  133. [4] => 6303
  134. [5] => 18718
  135. [6] => 27065
  136. [7] => 20636
  137. [8] => 285729
  138. [9] => 174906
  139. [10] => 354689
  140. )
  141.  
  142. )
  143.  
  144. )
  145. _POST Content:
  146. Array
  147. (
  148. [desc] => foothebar
  149. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement