public function actionIndex() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $files = UploadedFile::getInstances($model, 'file'); $images = []; foreach ($files as $file) { $_model = new UploadForm(); $_model->file = $file; if ($_model->validate()) { $filePath = 'uploads' . DIRECTORY_SEPARATOR . $_model->file->baseName . '.' . $_model->file->extension; $_model->file->saveAs($filePath); $images[] = ['source' => Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . $filePath, 'extension' => $_model->file->extension, 'name' => $_model->file->baseName]; } else { foreach ($_model->getErrors('file') as $error) { $model->addError('file', $error); } } } if ($model->hasErrors('file')) { $model->addError('file', count($model->getErrors('file')) . ' of ' . count($files) . ' files not uploaded'); } else { if ($images) { $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('images', 'direct', false, false, false); foreach ($images as $image) { $msg = new AMQPMessage(json_encode($image), ['delivery_mode' => 2]); $channel->basic_publish($msg, 'images', 'images routing key'); } $channel->close(); $connection->close(); } } } return $this->render('index', ['model' => $model]); }
use yii\bootstrap\ActiveForm; use yii\bootstrap\Button; use kartik\file\FileInput; // @var $this yii\web\View // @var $form yii\bootstrap\ActiveForm // @var $model \common\models\UploadForm $this->title = 'Firmwares'; $this->params['breadcrumbs'][] = $this->title; $dataProvider = new ActiveDataProvider(['query' => Yii::$app->user->can('admin') ? Firmware::find() : Firmware::findByUser(Yii::$app->user->identity->username), 'pagination' => ['pageSize' => 20]]); ?> <div class="box"> <div class="box-body"> <?php $form = ActiveForm::begin(['id' => 'upload-form', 'action' => '/firmware/upload', 'options' => ['enctype' => 'multipart/form-data', 'class' => 'form-inline']]); echo $form->field($model = new UploadForm(), 'file')->widget(FileInput::classname(), ['pluginOptions' => ['showPreview' => false, 'showCaption' => true, 'showRemove' => true, 'showUpload' => true, 'browseLabel' => 'Add firmware']]); ActiveForm::end(); ?> </div> </div> <div class="box"> <div class="box-body"> <?php if (Yii::$app->user->can('listResources')) { echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'description'], ['attribute' => 'manufacturer_id', 'value' => function ($model, $index, $widget) { return $model->manufacturer ? $model->manufacturer->name : ''; }], ['attribute' => 'model_number_id', 'value' => function ($model, $index, $widget) { return $model->modelNumber ? $model->modelNumber->value : ''; }], ['attribute' => 'device_type_id', 'value' => function ($model, $index, $widget) { return $model->deviceType ? $model->deviceType->name : '';