public function actionIndex($code = false) { $code = htmlspecialchars($code); $fileModel = FileRecord::find()->where(['code' => $code])->one(); if (!$fileModel) { throw new \yii\web\NotFoundHttpException('Такого файла не существует'); } $file_path = Yii::getAlias('@webroot') . $fileModel->file; //$file_path = 'D:/OpenServer/domains/yii/kscms/backend/web'.$fileModel->file; $file_info = pathinfo($file_path); header('Content-type: application/' . $file_info['extension']); header('Content-Disposition: attachment; filename="' . $fileModel->name . '.' . $file_info['extension'] . '"'); if (readfile($file_path)) { exit; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { if (!isset($params['parent_id'])) { throw new \Exception('Property parent_id must be setted'); } $query = FileRecord::find()->where(['parent_id' => $params['parent_id']]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]], 'pagination' => ['pageSize' => 10]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'parent_id' => $this->parent_id, 'active' => $this->active, 'sort' => $this->sort, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }