我正在使用 HTML5 AJAX 图像上传,我想在表单数据中添加一些额外信息,如 image_user_type 等,但我无法在 php 代码中找到 image_user_type 和其他的值。image_user_type 等是我必须创建的额外信息库不同类型/图像组的不同文件夹,例如 xyz grop pqr group 。任何帮助都应该不胜感激。
HTML
<form method="post" enctype="multipart/form-data" action="<?php echo Yii::app()->request->baseUrl?>/index.php/Feedback/FeedImage">
<input type="file" name="images" id="images" />
<input type="hidden" value="xyz" name="image_user_type" />
<button type="submit" id="btn"><?php echo Yii::t('strings','Upload Files!');?></button>
</form>
JS
<script type="text/javascript">
(function () {
var userimage=$("#waiter_img");
var input = document.getElementById("images"),
formdata = false;
function showUploadedItem (source) {
userimage.attr('src',source);
}
if (window.FormData) {
formdata = new FormData();
document.getElementById("btn").style.display = "none";
}
input.addEventListener("change", function (evt) {
//document.getElementById("response").innerHTML = ""
userimage.attr('src','<?php echo Yii::app()->request->baseUrl ?>/images/loader.gif');
var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
console.log(file);
formdata.append("images[]", file);
formdata.append("image_user_type",$("#image_user_type").val());
}
}
}
if (formdata) {
$.ajax({
url: "<?php echo Yii::app()->request->baseUrl?>/index.php/Feedback/FeedImage",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
}
});
}
}, false);
}());
PHP
public function actionFeedImage()
{
echo $image_type=$_POST['image_user_type'];
foreach ($_FILES["images"]["error"] as $key => $error) {
if( !is_dir(Yii::app()->basePath.'/../images/feedback') ) mkdir(Yii::app()->basePath.'/../images/feedback');
if( !is_dir(Yii::app()->basePath.'/../images/feedback/'.$image_type) ) mkdir(Yii::app()->basePath.'/../images/feedback/'.$image_type);
if ($error == UPLOAD_ERR_OK) {
$name = Yii::app()->session['admin_id']."-".date('Y-m-d H:i:s.')."-".$_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key],Yii::app()->basePath.'/../images/feedback/'.$image_type.'/'. $image_without_space);
}