while submitting large files using jquery ajax all input fields get empty on server site php -
i trying send large files server using jquery ajax , formdata. data received if send 7 files 1 mb each, if try send 10 files @ time input field received on server site empty, though input send server. following code file upload.
//php code public function multipleupload() { $title = $_post["title"]; $property_id = $_post["propertyid"]; $userid = $_post["userid"]; $albumid = $_post["albumid"]; $profileid = $_post["profile_id"]; if($albumid == "") { $album = new album; $album->title = $title; $album->property_id = $property_id; $album->user_id = $userid; $album->profile_id = $profileid; $album->save(); $albumid = $album->id; $userid = $album->user_id; $folder = str_replace(" ","-",$title. '-' . date("y-m-d_his")); if(!is_dir('uploads/album/'. $folder) || (!is_dir('uploads/album/'. $folder . "/original") && !is_dir('uploads/album/'. $folder . "/thumb"))) { mkdir('uploads/album/'. $folder); mkdir('uploads/album/'. $folder. "/original"); mkdir('uploads/album/'. $folder. "/thumb"); } } else { $album = db::table("photo_album")->select('*')->where('album_id', '=', $albumid)->orderby('id','desc')->first(); $folder = $album->file_path; $album = album::findorfail($albumid); $album->update(['title' => $title]); $albumid = $album->id; } $filecount = count($_files["finalfiles"]['name']); $photos = array(); for($i=0; $i < $filecount; $i++) { if($_post["rmimage"] == "" || !in_array($i,explode(",",$_post["rmimage"]))) { $imgfile = str_replace(" ","-",pathinfo($_files["finalfiles"]['name'][$i], pathinfo_filename))."-".date("y-m-d_his").".".pathinfo($_files["finalfiles"]['name'][$i], pathinfo_extension); $target_original_path = 'uploads/album/'. $folder . '/original/' . $imgfile; $target_thumb_path = 'uploads/album/'. $folder . '/thumb/' . $imgfile; if(move_uploaded_file($_files['finalfiles']['tmp_name'][$i], $target_original_path)) { $imagine = new imagine\gd\imagine(); $mode = imagine\image\imageinterface::thumbnail_inset; $thumb_size = new imagine\image\box(100, 100); $imagine->open($target_original_path) ->thumbnail($thumb_size, $mode) ->save($target_thumb_path); $size = new imagine\image\box(650, 350); $imagine->open($target_original_path) ->thumbnail($size, $mode) ->save($target_original_path); $filename = $_files["finalfiles"]["name"][$i]; //$album = album::where('user_id','=',$userid)->orderby('created_at','desc')->first(); $photo_details = ['album_id' => $albumid, 'property_id' => $property_id, 'file_name' => $filename, 'created_by' => $userid, 'updated_by' => $userid, 'url' => url::to('/').'/'.$target_original_path, 'thumb_url' => url::to('/').'/'.$target_thumb_path, 'file_path' => $folder]; array_push($photos, $photo_details); } else { echo '{"status":"failure","response":[],"message":"failed upload property image."}'; $status = "failure"; } } } if(count($photos) > 0) { $albumphoto = photoalbum::insert($photos); } } //jquery code $("#photoalbum").click(function() { if($("#title").val() == '') { $(".error-title").show(); $(".error-photo").hide(); } else { if($("#preview-area ul li").size() == 0) { $(".error-title").hide(); $(".error-photo").show(); } else { $("#photoalbum").attr("disabled", true); var form = $('form')[0]; // need use standart javascript object here var formdata = new formdata(form); $.ajax({ url: "api/multipleupload", type: "post", data: formdata, cache: false, contenttype: false, processdata: false, success: function(result){ $state.go('albums',{propertyid: $scope.propertyid}); toastr.success('album has been added successfully.'); } }); } } });
in past have experienced issues uploading files using jquery ajax , formdata weird outputs in server side (my question).
what solved problem avoiding jquery , take more vanilla approach using xmlhttprequest()
so:
function save(action){ var formelement = document.getelementbyid("form"); var formdata = new formdata(formelement); formdata.append('_token', $('meta[name="csrf-token"]').attr('content')); var request = new xmlhttprequest(); request.open("post", "{{url('myurl')}}"); request.send(formdata); request.onload = function(oevent) { if (request.status == 200) { toastr.success('yai! saved successfully!'); } else { toastr.error('oh oh! went wrong!'); } $( "#form-wrapper" ).toggleclass( "be-loading-active" ); }; }
and in controller access files in request object:
public function store(request $request) { dd($request->nameofinput); }
also, people suggested in comments, if handling large files make sure php parameters configured accordingly in php.ini
.
hope helps you.
Comments
Post a Comment