FilesController.php 4.4 KB

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