php - Upload 2 images in an HTML form -
i trying upload 2 images part of html form. right php script relying on name of elements being same, can reuse code both images, without having change 1 piece.
upload.php
:
<?php define("image_directory", "images"); uploadfile("file"); uploadfile("file_1"); function uploadfile($name) { //if directory images not exist, create if(!is_dir(image_directory)) { mkdir(image_directory, 0777, true); } var_dump(move_uploaded_file($_files[$name]["tmp_name"], image_directory . "\\" .basename($_files[$name]["name"]))); } ?>
my html form:
<form id="create_card_form" enctype="multipart/form-data" method="post"> front text: <input type="text" name="front_text"><br><br> front media (image or video link) <input type="text" name="front_media"><br><br> <input type='file' name='file' id='image_uploaded_front' accept='image'/><br><br> text: <input type="text" name="back_text"><br><br> media (image or video link) <input type="text" name="back_media"><br><br> <input type='file' name='file' id='image_uploaded_back' accept='image'/><br><br> <input type="submit" value="create card" onclick="createcard(this.form)"> </form>
ajax
:
var form_data = new formdata(); var form_data_front = new formdata($('#create_card_form')[0]); var form_data_back = new formdata($('#create_card_form')[1]); form_data.append('file', form_data_front); form_data.append('file_1', form_data_back); $.ajax({ url: "upload.php", data : form_data, type : "post", async: false, cache: false, contenttype: false, processdata: false, datetype : "html", });
however, when use form, second image taken in place of first image, assuming because named same. however, changing names result in need add lot of duplicate code process images. there way same name?
update: edited javascript , php script based on answers question. now, seems problem getting images php script properly. right now, code have, there error in uploading images in php.
the answer question no - can't post
properties same name.
the solution problem you're trying achieve, not in way you're trying achieve it. don't want duplicated code (which good) - put code needs ran more once function.
<?php uploadfile("file"); uploadfile("secondfile"); function uploadfile($name) { define("image_directory", "images"); //if directory images not exist, create if(!is_dir(image_directory)) { mkdir(image_directory, 0777, true); } var_dump(move_uploaded_file($_files[$name]["tmp_name"], image_directory . "\\" .basename($_files[$name]["name"]))); } ?>
edit: spotted alex howansky beat me answer in comment.
Comments
Post a Comment