Assalamualaikum Wr. Wb. Good afternoon all. In this session, I would like to explain upload file or image on Yii2 application. Now, i use default Yii2 upload file.
I still using yii2basic application that created before. If you haven't develop that, please prefer this link to develop.
First, plase running this query or add fields foto to your mahasiswa table :
Second, please open your Mahasiswa model class and change with following the content :
If you found error message "The fileinfo PHP extension is not installed" on your application, please actived php_fileinfo extensions on php.ini and restart your apache.
Now, you have been implemented file upload to your Yii2 application. I hope my tutorial can help you anymore. So good luck. Thank you. Wassalamualaikum Wr. Wb.
I still using yii2basic application that created before. If you haven't develop that, please prefer this link to develop.
First, plase running this query or add fields foto to your mahasiswa table :
alter table mahasiswa add foto varchar(50);Then, create uploads direcoty on yii2basic/web for save the file upload.
Second, please open your Mahasiswa model class and change with following the content :
namespace app\models;Third, open your MahasiswaController class in controllers directory and change with following the content :
use Yii;
class Mahasiswa extends \yii\db\ActiveRecord{
public function rules()
{
return [
[['foto'],'required','on' => 'create'],
[['foto'],'file','extensions'=>'jpg, jpeg, png', 'maxSize'=>1024 * 1024 * 1],
];
}
public function attributeLabels()
{
return [
'foto' => 'Foto',
];
}
}
use Yii;Fourd, open your _form file in views/mahasiswa directory and change with following the content:
use yii\web\UploadedFile;
class MahasiswaController extends Controller{
//another code
public function actionCreate()
{
$model = new Mahasiswa(['scenario' => 'create']);
if ($model->load(Yii::$app->request->post())) {
try{
$picture = UploadedFile::getInstance($model, 'foto');
$model->foto = $_POST['Mahasiswa']['nim'].'.'.$picture->extension;
if($model->save()){
$picture->saveAs('uploads/' . $model->nim.'.'.$picture->extension);
Yii::$app->getSession()->setFlash('success','Data saved!');
return $this->redirect(['view','id'=>$model->nim]);
}else{
Yii::$app->getSession()->setFlash('error','Data not saved!');
return $this->render('create', [
'model' => $model,
]);
}
}catch(Exception $e){
Yii::$app->getSession()->setFlash('error',"{$e->getMessage()}");
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
//another code
}
$form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]);For displaying the picture on DetailView, please following the content :
//another code
echo $form->field($model, 'foto')->fileInput();
//another code
echo DetailView::widget([Ok, you finished this code. And then you must trial yii2basic application. Access your yii2basic, and you trial create new data.
'model' => $model,
'attributes' => [
'nim',
'nama',
'jurusan',
'angkatan',
'alamat:ntext',
[
'label'=>'Foto',
'format'=>'raw',
'value'=>Html::img(Yii::$app->request->baseUrl.'/uploads/'.$model->foto,['width'=>'100px']),
],
],
]);
If you found error message "The fileinfo PHP extension is not installed" on your application, please actived php_fileinfo extensions on php.ini and restart your apache.
Now, you have been implemented file upload to your Yii2 application. I hope my tutorial can help you anymore. So good luck. Thank you. Wassalamualaikum Wr. Wb.