php - json decode file names when uploading multiple images and loop through them -
i using plugin ( have checked manual if there 1 this) json_encodes() file names when uploading multiple images. have far works 1 image only.
$response = array(); $message = ""; $files = $_post['files']; foreach (json_decode($files) $key => $value) { $image = $value->file_name; } if($_post['files'] == "[]") { $message .= "at least 1 image required"; } if($message) { $response['success'] = false; $response['message'] = $message; } else { $response['success'] = true; $response['message'] = $image; } echo json_encode($response);
this jquery/ajax
var uploader = $('.picker-uploader').uploader({ upload_url: 'upload.php', file_picker_url: 'files.php', input_name: 'file', maximum_total_files: 5, maximum_file_size: 50009000, file_types_allowed: ['image/jpeg', 'image/png'], on_error: function(err) { swal({ title: "upload failed", text: err.messages, type: "warning" }) } }) $( "#edit_pic" ).on("click", function(e) { e.preventdefault(); var files = $('input[name="file"]').val(); $.ajax({ url: 'functions/add-support-images.php', type: 'post', datatype: 'json', data: {files: files}, beforesend: function() { $( "#edit_pic" ).prop("disabled", true).html("uploading..."); }, }) .done(function(data) { if(!data.success) { console.log(data.message); } else { alert(data.message); } })
the upload button in tab:
<div id="addimages" class="tab-pane"> <div class="form-group"> <p></p> <label>additional images</label> <p></p> <button type="button" class="picker-uploader btn btn-primary">select images</button> </div> <p></p> <button type="submit" name="edit_pic" class="btn btn-fill btn-info" id="edit_pic">update</button> <p></p> <div class="alert alert-danger" id="image_error"></div> <div class="alert alert-success" id="image_success"></div> </div> </div>
i want json_encode() again if choose more 1 file outputs last uploaded file. need loop through $image not sure how achieve that.
you sending 1 value of first input field using code:
var files = $('input[name="file"]').val();
try change:
var files = $('input[name="file"]').val();
to:
var files = $('input[name="file"]').map(function() { return $( ).val(); }).get();
edit
the code of uploaded files recursing works fine, however, have decide how going handle files. either collect filenames, or files. example, if want store filenames, can this:
$response = array(); $message = ""; $images = array(); $files = $_post['files']; foreach (json_decode($files) $key => $value) { $images[] = $value->file_name; } print_r($images) // see contents of array
when do
$image = $value->file_name;
you overriding $image
variable, contains previous filename loaded foreach
loop.
Comments
Post a Comment