| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace app\controllers;
- use Yii;
- use app\models\Files;
- use app\models\FilesSearch;
- use yii\filters\VerbFilter;
- use yii\web\NotFoundHttpException;
- class FilesController extends \yii\web\Controller
- {
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
- public function init()
- {
- //if(Yii::$app->user->isGuest)
- //{
- // return $this->redirect('/user/login');
- //}
- }
- public function actionDownload($id)
- {
- $model = $this->findModel($id);
- if(!is_file($model->path.$model->name)){
- throw new NotFoundHttpException('Файл не знайдено, можливо він був видалений .');
- }
- //$filename = str_replace(" ","_",$model->name);
- $file = $model->path . $model->name; //print $file; exit;
- /*
- header('Content-Description: File Transfer');
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename=' . $model->name);
- header('Content-Transfer-Encoding: binary');
- header('Expires: 0');
- header('Cache-Control: must-revalidate');
- header('Pragma: public');
- header('Content-Length: ' . filesize($file));
- readfile($file);*/
- Yii::$app->response->sendFile($file);
- // Yii::$app->response->xSendFile($file);
- /*
- header ("Content-Type: application/octet-stream");
- header ("Accept-Ranges: bytes");
- header ("Content-Length: ".filesize($file));
- header ("Content-Disposition: attachment; filename=" . $model->name);
- readfile($file);
- */
- }
- /* public function actionIndex()
- {
- return $this->render('index');
- }*/
- protected function findModel($id)
- {
- if (($model = Files::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- /**
- * Lists all Files models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new FilesSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Files model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Files model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new Files();
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Updates an existing Files 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);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Deletes an existing Files model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * Finds the Files model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Files the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- }
|