php - submit array of files via ajax form -
i have form allow me submit text + number of files. form submitted ajax.
because it's number of files upload function give me error:
warning: move_uploaded_file(images/usersfiles/14367317720-101.jpg) [function.move-uploaded-file]: failed open stream: no such file or directory in c:\program files (x86)\wamp\www\new-site\func\global.func.php on line 134
line 134 is:
if (move_uploaded_file($files['file']['tmp_name'][$i], user_files.$files['file']['name'][$i]))
files' var should array (because can load number of files).
how can fix error?
btw - when function "upload_files" got 1 file, , "foreach" removed, succeed upload file...
define("user_files", "images/usersfiles/");
html:
<form class="form-horizontal" action='#' method="post" id="addcommentform" enctype="multipart/form-data"> <textarea class="form-control" name="post[text]"></textarea> <input type='file' name='file[]' class='multi form-control' maxlength='1' accept='gif|jpg|png|bmp' id="files"/> <a class="btn btn-primary" id="submit">submit</a> </form>
js:
$(function() { $("#submit").click(function() { var file_data = $('#files').prop('files')[0]; var form_data = new formdata(); form_data.append('file[]', file_data); var files_data = form_data; var act = 'add'; form_data.append('act', act); form_data.append('post[text]', $("#addcommentform").find("textarea").val()); $.ajax({ type: "post", url: "ajax/addpost.php", datatype: 'text', cache: false, contenttype: false, processdata: false, data: form_data, success: function(data) { $('#commentsbox').html(data); $("#addcommentform")[0].reset(); } }); return false; // avoid execute actual submit of form. }); });
server:
function upload_files ($ownerid, $msg, $files, $type) { $datelx = get_current_linuxtime(); ///////// upload files ////////////// if(!empty($files)) { foreach($files['file']['name'] $i => $filename) { $filesurffix = pathinfo ($_files['file']['name'][$i]); $filesurffix = $filesurffix['extension']; $files['file']['name'][$i] = str_replace(' ','',$files['file']['name'][$i]); $files['file']['name'][$i] = $datelx.$i."-".$ownerid.".".$filesurffix; $filename = $files['file']['name'][$i]; if (move_uploaded_file($files['file']['tmp_name'][$i], user_files.$files['file']['name'][$i])) { $uploadfilesquery = "insert `files` (ownerid, name, type) values('$ownerid', '$filename', '$type')"; $res = mysql_query($uploadfilesquery); if (!$res) $msg['error']['uploadfile'] = "error <br />".mysql_error(); } elseif ($files['file']['error'][$i] != 4) $msg['error']['uploadfile'] = "error "; } } return ($msg); }
Comments
Post a Comment