FilesController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use app\models\Files;
  5. use app\models\FilesSearch;
  6. use yii\filters\VerbFilter;
  7. use yii\web\NotFoundHttpException;
  8. class FilesController extends \yii\web\Controller
  9. {
  10. /**
  11. * @inheritdoc
  12. */
  13. public function behaviors()
  14. {
  15. return [
  16. 'verbs' => [
  17. 'class' => VerbFilter::className(),
  18. 'actions' => [
  19. 'delete' => ['POST', 'GET'],
  20. ],
  21. ],
  22. ];
  23. }
  24. public function init()
  25. {
  26. parent::init();
  27. //if(Yii::$app->user->isGuest)
  28. //{
  29. // return $this->redirect('/user/login');
  30. //}
  31. }
  32. public function actionDownload($id)
  33. {
  34. $model = $this->findModel($id);
  35. if(!is_file($model->path.$model->name)){
  36. throw new NotFoundHttpException('Файл не знайдено, можливо він був видалений .');
  37. }
  38. //$filename = str_replace(" ","_",$model->name);
  39. $file = $model->path . $model->name; //print $file; exit;
  40. /*
  41. header('Content-Description: File Transfer');
  42. header('Content-Type: application/octet-stream');
  43. header('Content-Disposition: attachment; filename=' . $model->name);
  44. header('Content-Transfer-Encoding: binary');
  45. header('Expires: 0');
  46. header('Cache-Control: must-revalidate');
  47. header('Pragma: public');
  48. header('Content-Length: ' . filesize($file));
  49. readfile($file);*/
  50. Yii::$app->response->sendFile($file);
  51. // Yii::$app->response->xSendFile($file);
  52. /*
  53. header ("Content-Type: application/octet-stream");
  54. header ("Accept-Ranges: bytes");
  55. header ("Content-Length: ".filesize($file));
  56. header ("Content-Disposition: attachment; filename=" . $model->name);
  57. readfile($file);
  58. */
  59. }
  60. /* public function actionIndex()
  61. {
  62. return $this->render('index');
  63. }*/
  64. protected function findModel($id)
  65. {
  66. if (($model = Files::findOne($id)) !== null) {
  67. return $model;
  68. } else {
  69. throw new NotFoundHttpException('The requested page does not exist.');
  70. }
  71. }
  72. /**
  73. * Lists all Files models.
  74. * @return mixed
  75. */
  76. public function actionIndex()
  77. {
  78. $searchModel = new FilesSearch();
  79. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  80. return $this->render('index', [
  81. 'searchModel' => $searchModel,
  82. 'dataProvider' => $dataProvider,
  83. ]);
  84. }
  85. /**
  86. * Displays a single Files model.
  87. * @param integer $id
  88. * @return mixed
  89. */
  90. public function actionView($id)
  91. {
  92. return $this->render('view', [
  93. 'model' => $this->findModel($id),
  94. ]);
  95. }
  96. /**
  97. * Creates a new Files model.
  98. * If creation is successful, the browser will be redirected to the 'view' page.
  99. * @return mixed
  100. */
  101. public function actionCreate()
  102. {
  103. $model = new Files();
  104. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  105. return $this->redirect(['view', 'id' => $model->id]);
  106. } else {
  107. return $this->render('create', [
  108. 'model' => $model,
  109. ]);
  110. }
  111. }
  112. /**
  113. * Updates an existing Files model.
  114. * If update is successful, the browser will be redirected to the 'view' page.
  115. * @param integer $id
  116. * @return mixed
  117. */
  118. public function actionUpdate($id)
  119. {
  120. $model = $this->findModel($id);
  121. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  122. return $this->redirect(['view', 'id' => $model->id]);
  123. } else {
  124. return $this->render('update', [
  125. 'model' => $model,
  126. ]);
  127. }
  128. }
  129. /**
  130. * Deletes an existing Files model.
  131. * If deletion is successful, the browser will be redirected to the 'index' page.
  132. * @param integer $id
  133. * @return mixed
  134. */
  135. public function actionDelete($id)
  136. {
  137. $model = Files::findOne($id);
  138. if ($model) {
  139. $model->delete();
  140. }
  141. return $this->redirect(Yii::$app->request->referrer);
  142. }
  143. /**
  144. * Finds the Files model based on its primary key value.
  145. * If the model is not found, a 404 HTTP exception will be thrown.
  146. * @param integer $id
  147. * @return Files the loaded model
  148. * @throws NotFoundHttpException if the model cannot be found
  149. */
  150. }