javascript - All files are not getting from file upload control using jquery -
i facing problem during images upload.
i have input type file control in form , more input controls can added using jquery. problem this: when control values, return first value file control.
how can added files in control? here code:
javascript
$(document).ready(function() { $('#add_more').click(function() { $(this).before($("<div/>", { id: 'filediv' }).fadein('slow').append( $("<input/>", { name: 'file[]', type: 'file', id: 'file', accept: '.jpg, .jpeg, .gif' }), $("<br/><br/>") )); }); $('#upload').click(function(e) { var name = $(":file").val(); if (!name) { alert("file must selected"); e.preventdefault(); } else { //upload images var filedata = $('#file').prop("files")[0]; var form_data = new formdata(); form_data.append('file', filedata); $.ajax({ url: "myurl.php", datatype: 'text', data: form_data, cache: false, contenttype: false, processdata: false, type: "post", error: function() { alert('error'); }, success: function(ret) { alert('sucess'); } }); } } });
html
<form enctype="multipart/form-data" action="" method="post"> <hr/> <div id="filediv"> <input name="file[]" type="file" id="file" accept=".jpg, .gif, .jpeg" /> </div> <br/> <input type="hidden" value="" id="class" name="class"> <input type="button" id="add_more" class="upload" value="add more files" /> <input type="button" value="upload file" name="submit" id="upload" class="upload" /> </form>
when post php using $_files['file']['name']
, counting using count($_files['file']['name'])
returns 1
.
when process further, first file uploaded in corresponding folder.
wrong on line var filedata = $('#file').prop("files")[0];
.
your js code should below :
$(document).ready(function () { $('#add_more').click(function () { $(this).before($("<div/>", { id: 'filediv' }).fadein('slow').append( $("<input/>", { name: 'file[]', type: 'file', // id must unique // id: 'file', class: "file_input", accept: '.jpg, .jpeg, .gif' }), $("<br/><br/>") )); }); $('#upload').click(function (e) { var boolareallfilesselected = true; var objformdata = new formdata(); $.each($(":file"), function ( ) { if (!$(this).val()) { alert("file must selected"); $(this).focus(); return boolareallfilesselected = false; } else { objformdata.append('file[]', $(this).prop("files")[0]); } }); if (boolareallfilesselected) { $.ajax({ url: "myurl.php", datatype: 'text', data: objformdata, cache: false, contenttype: false, processdata: false, type: "post", error: function () { alert('error'); }, success: function (ret) { alert('sucess'); } }); } }); });
your php code should below :
<?php function uploadsingleimage($arrsinglefile = array()) { if (!empty($arrsinglefile)) { // here image uploading code } } if (!empty($_files['file'])) { $arrfile = $_files['file']; foreach ($arrfile['name'] $intindex => $strname) { $arrsinglefile["name"] = $strname; $arrsinglefile["type"] = $arrfile['type'][$intindex]; $arrsinglefile["tmp_name"] = $arrfile['tmp_name'][$intindex]; $arrsinglefile["error"] = $arrfile['error'][$intindex]; $arrsinglefile["size"] = $arrfile['size'][$intindex]; uploadsingleimage($arrsinglefile); } } else { die("you have not uploaded file."); }
Comments
Post a Comment