1 <?php
2
3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19
20 class FileController extends Controller {
21
22 23 24 25 26
27 public function actions() {
28 return array(
29 'image' => array(
30 'class' => 'p3media.actions.P3MediaImageAction',
31 ),
32 );
33 }
34
35 public function beforeAction($action) {
36 parent::beforeAction($action);
37 if (isset($_GET['path'])) {
38 $model = P3Media::model()->findByAttributes(array('path' => urldecode($_GET['path'])));
39 if ($model !== null) {
40 $_GET['id'] = $model->id;
41 } else {
42 $_GET['id'] = 0;
43
44 }
45 }
46 return true;
47 }
48
49 50 51
52 public function actionIndex() {
53
54 if (!$_GET['id']) {
55 throw new CException('No file specified.');
56 } else {
57 $model = P3Media::model()->findByPk($_GET['id']);
58 $filename = Yii::getPathOfAlias($this->module->dataAlias) . DIRECTORY_SEPARATOR . $model->path;
59 if (!is_file($filename)) {
60 throw new CException('File not found.');
61 } else {
62 header('Content-Disposition: attachment; filename="' . $model->title . '"');
63 header('Content-type: ' . $model->mimeType);
64 readfile($filename);
65 exit;
66 }
67 }
68 }
69 }