I am really stuck and not really sure on how to move forward. (a nice example of code for this situation would be awesome I think it would be useful to me and many others)
I want a form which say has an upload field for featured image and then another for say header image as separate fields so that in the frontend I always know that image 1 is featured and 2 is header (that if they are being saved as an array)
Before is my current setup that works for single file upload
Model
Some code above
public function getImageFile()
{
return isset($this->map_icon) ? Yii::$app->params['uploadPath'] . $this->map_icon : null;
}
/**
* fetch stored image url
* @return string
*/
public function getImageUrl()
{
// return a default image placeholder if your source avatar is not found
$map_icon = isset($this->map_icon) ? $this->map_icon : 'default_user.jpg';
return Yii::$app->params['uploadUrl'] . $map_icon;
}
/**
* Process upload of image
*
* @return mixed the uploaded image instance
*/
public function uploadImage() {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array (you may need to use
// getInstances method)
$image = UploadedFile::getInstance($this, 'image');
// if no image was uploaded abort the upload
if (empty($image)) {
return false;
}
// store the source file name
$this->map_iconfilename = $image->name;
$ext = end((explode(".", $image->name)));
// generate a unique file name
$this->map_icon = Yii::$app->security->generateRandomString().".{$ext}";
// the uploaded image instance
return $image;
}
/**
* Process deletion of image
*
* @return boolean the status of deletion
*/
public function deleteImage() {
$file = $this->getImageFile();
// check if file exists on server
if (empty($file) || !file_exists($file)) {
return false;
}
// check if uploaded file can be deleted on server
if (!unlink($file)) {
return false;
}
// if deletion successful, reset your file attributes
$this->avatar = null;
$this->filename = null;
return true;
}
View
<?php
$form = ActiveForm::begin([
'options'=>['enctype'=>'multipart/form-data'] // important
]);
?>
Some field,,,,,,
<?php
echo $form->field($model, 'map_iconfilename');
// display the image uploaded or show a placeholder
// you can also use this code below in your `view.php` file
$title = isset($model->map_iconfilename) && !empty($model->map_iconfilename) ? $model->map_iconfilename : 'map_icon';
echo Html::img($model->getImageUrl(), [
'class'=>'img-thumbnail',
'alt'=>$title,
'title'=>$title
]);
// your fileinput widget for single file upload
echo $form->field($model, 'image')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*'],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
]]);
?>
Controller
public function actionCreate()
{
$model = new ShopDirectory();
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$image = $model->uploadImage();
if ($model->save()) {
// upload only if valid uploaded file instance found
if ($image !== false) {
$path = $model->getImageFile();
$image->saveAs($path);
}
return $this->redirect(['view', 'id'=>$model->id]);
} else {
// error in saving model
}
}
return $this->render('create', [
'model'=>$model,
]);
}
/**
* Updates an existing ShopDirectory model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$oldFile = $model->getImageFile();
$oldmap_icon = $model->map_icon;
$oldmap_iconFileName = $model->map_iconfilename;
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$image = $model->uploadImage();
// revert back if no valid file instance uploaded
if ($image === false) {
$model->map_icon = $oldmap_icon;
$model->map_iconfilename = $oldmap_iconFileName;
}
if ($model->save()) {
// upload only if valid uploaded file instance found
if ($image !== false && unlink($oldFile)) { // delete old and overwrite
$path = $model->getImageFile();
$image->saveAs($path);
}
return $this->redirect(['view', 'id'=>$model->id]);
} else {
// error in saving model
}
}
return $this->render('update', [
'model'=>$model,
]);
}