Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- index.html -->
- <html>
- <head>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
- </head>
- <body>
- <form id="myform" onsubmit="return uploadFiles(this);">
- <input type="text" name="desc" />
- <input type="file" name="somefile" multiple="1" />
- <input type="submit" value="Go" />
- </form>
- <div id="status">Upload file status</div>
- </body>
- <script>
- // Function to upload a form via FormData, breaking out files and cutting
- // any non-named elements. Assumes that there's a #status DIV and the
- // URL is hardcoded.
- function uploadFiles(frm) {
- var formdata = new FormData();
- // I'm doing this to separate out the upload content. Note that multiple
- // files can be uploaded and will appear as a decently-friendly PHP array
- // in $_FILES.
- $(frm).find(":input").each(function(idx, ele) {
- // This is a file field. Break it out.
- if(ele.files) {
- for(i=0; i<ele.files.length; i++) {
- formdata.append(ele.name + "[" + i + "]", ele.files[i]);
- }
- // Not a file element, so put in the upload iff there's a name.
- } else if(ele.name) {
- formdata.append(ele.name, ele.value);
- }
- });
- // Run the AJAX.
- $.ajax({
- url: "test.php", // Change Me :-)
- type: "POST",
- data: formdata,
- processData: false,
- contentType: false,
- success: function(data) {
- $("#status").html(data);
- },
- error: function(xhr, status, error) {
- $("#status").html("Error uploading file(s): " + error);
- },
- });
- return false;
- }
- </script>
- </html>
- <!-- test.php -->
- <pre>
- _FILES Content:
- <?php print_r($_FILES) ?>
- _POST Content:
- <?php print_r($_POST) ?>
- </pre>
- <!-- sample output -->
- _FILES Content:
- Array
- (
- [somefile] => Array
- (
- [name] => Array
- (
- [0] => 210.jpg
- [1] => astoria-desat.jpg
- [2] => build-desat.jpg
- [3] => control-panel.jpg
- [4] => cuda.png
- [5] => departure.png
- [6] => dietary.png
- [7] => embarassment.png
- [8] => error.png
- [9] => europe.jpg
- [10] => europe.png
- )
- [type] => Array
- (
- [0] => image/jpeg
- [1] => image/jpeg
- [2] => image/jpeg
- [3] => image/jpeg
- [4] => image/png
- [5] => image/png
- [6] => image/png
- [7] => image/png
- [8] => image/png
- [9] => image/jpeg
- [10] => image/png
- )
- [tmp_name] => Array
- (
- [0] => /tmp/phpRPsOcp
- [1] => /tmp/phpY0SrcE
- [2] => /tmp/phpGOS8bT
- [3] => /tmp/phpvi4Yb8
- [4] => /tmp/phpvsjlcn
- [5] => /tmp/phpEVuKcC
- [6] => /tmp/phpCQzbdR
- [7] => /tmp/phpouEHd6
- [8] => /tmp/phpK16gel
- [9] => /tmp/phpvejCfA
- [10] => /tmp/php9KJdhP
- )
- [error] => Array
- (
- [0] => 0
- [1] => 0
- [2] => 0
- [3] => 0
- [4] => 0
- [5] => 0
- [6] => 0
- [7] => 0
- [8] => 0
- [9] => 0
- [10] => 0
- )
- [size] => Array
- (
- [0] => 22650
- [1] => 40447
- [2] => 21591
- [3] => 45202
- [4] => 6303
- [5] => 18718
- [6] => 27065
- [7] => 20636
- [8] => 285729
- [9] => 174906
- [10] => 354689
- )
- )
- )
- _POST Content:
- Array
- (
- [desc] => foothebar
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement