I am trying to create a simple jquery file upload following the steps in this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/ and it is fully functional and excellent. However I want to have more control and more security over what users can upload images, for example I want to be able to send a token as well. Here is my original code:
$('#images').change(function (evt) {
$('#response').text("Uploading . . .");
var amount = this.files.length;
var reader, file;
alert(formdata);
for (var i = 0; i < amount; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
appendUploadedPic(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "php/upload.php",
type: "POST",
data: formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res['image_name']);
}
});
}
});
and the php is simple so far
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../members/" . $_FILES['images']['name'][$key]);
}
}
$respond = array("filename" => $_FILES["images"]["name"][0], "type" => "image");
echo json_encode($respond);
now what I want to do is this
$.ajax({
url: "php/upload.php",
type: "POST",
data: 'token=someToken&code=upload&data='+formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res);
}
});
Is it possible? If so, how would I read it from php? It its a post method i normally use a $_POST['code'] and $_POST'token'], but how would I read the images formdata object?