Skip to content

Instantly share code, notes, and snippets.

@alexbabintsev
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbabintsev/972d1def5a894b617877 to your computer and use it in GitHub Desktop.
Save alexbabintsev/972d1def5a894b617877 to your computer and use it in GitHub Desktop.
Yii2: загрузка файлов
use yii\base\DynamicModel;
public function actionCreate()
{
$model = new DynamicModel([
'name', 'file_id'
]);
// behavior untuk upload file
$model->attachBehavior('upload', [
'class' => 'mdm\upload\UploadBehavior',
'attribute' => 'file',
'savedAttribute' => 'file_id', // coresponding with $model->file_id
// 'uploadPath' => '@common/upload', // saved directory. default to '@runtime/upload'
'autoSave' => true, // when true then uploaded file will be save before ActiveRecord::save()
'autoDelete' => true, // when true then uploaded file will deleted before ActiveRecord::delete()
]);
// rule untuk model
$model->addRule('name', 'string')
->addRule('file', 'file', ['extensions' => 'jpg']);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->saveUploadedFile() !== false) {
Yii::$app->session->setFlash('success', 'Upload Sukses');
}
}
return $this->render('upload', ['model' => $model]);
}
<?php
use yii\base\DynamicModel;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
?>
<div>
<?php
$form = ActiveForm::begin([
'options' => [ 'enctype' => 'multipart/form-data']
]);
?>
<?= $form->field($model, 'name'); ?>
<?= $form->field($model, 'file')->fileInput(); ?>
<?php if ($model->file_id): ?>
<div class="form-group">
<?= Html::img(['/file', 'id' => $model->file_id]) ?>
</div>
<?php endif; ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
/* Include debug functions */
require_once(__DIR__.'/functions.php');
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'posts/<id:\d+>' => 'posts/view',
'posts' => 'posts/index',
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'yhYFOIvo4xkZG0NEB7BiAfXdPPmtq6mi',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
'assetManager' => [
'class' => 'yii\web\AssetManager',
'bundles' => [
'yii\web\JqueryAsset' => [
// 'jsOptions' => ['position' => 1],
'js' => [
YII_ENV_DEV ? 'jquery.js' : 'jquery.min.js'
]
],
'yii\bootstrap\BootstrapAsset' => [
'css' => [
YII_ENV_DEV ? 'css/bootstrap.css' : 'css/bootstrap.min.css',
]
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js' => [
YII_ENV_DEV ? 'js/bootstrap.js' : 'js/bootstrap.min.js',
]
]
],
],
],
'controllerMap' => [
'file' => 'mdm\\upload\\FileController', // use to show or download file
],
// 'defaultRoute' => 'posts/',
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment