Get Mystery Box with random crypto!

Controllerdagi kod public function actionCreate() { | For Programming

Controllerdagi kod

public function actionCreate()
{
if (Yii::$app->request->isAjax) {
$model = new Product();
$product_price = new ProductPrice();
$currency = Currency::find()->where(['status' => Currency::STATUS_ACTIVE])->asArray()->all();
$currency_list = ArrayHelper::map($currency, 'id', 'title');
$category = Category::find()->where(['status' => Category::STATUS_ACTIVE])->asArray()->all();
$category_list = ArrayHelper::map($category, 'id', 'title');
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if ($model->load(Yii::$app->request->post())) {
$model->imageFile = $_FILES;
if ($model->Upload() && $model->save()) return ['status' => 'success'];
}
return [
'status' => 'error',
'content' => $this->renderAjax('_form.php',[
'model' => $model,
'product_price' => $product_price,
'currency_list' => $currency_list,
'category_list' => $category_list,
]),
];
}
}


Modeldagi kod


public function getExtension()
{
$extension = explode("/", $this->imageFile['imageFile']['type']);
return $extension[1];
}

public function Upload()
{
if ($this->validate()) {
$file_name = date("Y-m-d")."_".rand(1000, 9999) . '.' . $this->getExtension();
move_uploaded_file($this->imageFile['imageFile']['tmp_name'], 'uploads/images/' . $file_name);
$this->image = $file_name;
return true;
}
return false;
}

Jquerydagi kod


$(document).on("click", "#insert-product-form",function(){
var data = new FormData();
var form = $('#product-create-form').serializeArray();
$.each(form, function (key, input) {
data.append(input.name, input.value);
});
var file_data = $("#product-imagefile").prop("files")[0];
data.append("imageFile", file_data);
console.log(data)
$.ajax({
url: '/product/create',
dataType: 'JSON',
type: 'post',
data: data,
contentType: false,
processData: false,
success: function(response){
if (response.status == 'error') {
$('#exampleModal .modal-title').html('Tovar qo\'shish').css({fontSize: 23})
$('#exampleModal .modal-body').html(response.content)
} else if (response.status == 'success') {
$.pjax.reload('#id-pjax')
$(".close").trigger("click");
}
}
});
});


Ajax orqali file yuklanayotganda file malumotlari kelmay qoladi xattoki $_FILES o'zgaruvchisida ham shuning uchun manashu usulda malumotlarni saqlab olsa bo'ladi