SWFUploadAction.php
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* 支持onBeforUpload和onAfterUpload事件
* 同SWFUpload Widget一起使用
*
* @version 1.0.2 2010.10.19 14:08
*/
class SWFUploadAction extends CAction
{
public $path='';//文件路径 c:/wamp/www/a.EXT
protected $callbackJS = '';
public function run()
{
$this->init();
$filepath = $this->upload();
exit();
}
public function onAfterUpload($event)
{
$this->raiseEvent('onAfterUpload',$event);
}
public function onBeforeUpload($event)
{
$this->raiseEvent('onBeforeUpload',$event);
}
protected function init()
{
if(!isset($_POST['SWFUpload']))
{
Yii::app()->getRequest()->redirect(Yii::app ()->homeUrl);
return ;
}
$this->callbackJS = isset($_POST['callbackJS'])?$_POST['callbackJS']:'';
if($this->path ==='')
{
throw new Exception('文件路径没有指定!');
}
// // 删除上一个临时文件
// if(isset($_SESSION['temp_file'])&&is_file($_SESSION['temp_file'])&&(intval($_POST['fileQuenueLimit']) == 1))
// {
// unlink($_SESSION['temp_file']); //删除swfupload 的临时文件
// }
}
// protected function upload()
// {
// $file = CUploadedFile::getInstanceByName('Filedata');
//
// $this->onBeforeUpload(new CEvent(array('uploadedFile'=>&$file)));
// $this->filepath = str_replace('.EXT','.'.$file->extensionName,$this->filepath);
//
// $filename = substr(strrchr($this->filepath,'/'),1);
// $this->filepath = str_replace('\\','/',$this->filepath);
// $filedir = str_replace(array("/$filename",Yii::app()->params['uploadDir']),'',$this->filepath);
//
// if(!is_dir(Yii::app()->params['uploadDir'].$filedir))
// {
// mkdir(Yii::app()->params['uploadDir'].$filedir, 0777,true);
// }
// $file->saveAs($this->filepath);
// $_SESSION['temp_file'] = $this->filepath;
// echo 'JS:('.$this->callbackJS.")('$filename','$filedir','{$file->getName()}');";
// $this->onAfterUpload(new CEvent(array('uploadedFile'=>&$file,'name'=>$filename,'path'=>$filedir)));
// return $this->filepath;
// }
protected function upload()
{
$file = CUploadedFile::getInstanceByName('Filedata');
$this->onBeforeUpload(new CEvent(array('uploadedFile'=>&$file)));
$rootPath = realpath(Yii::app()->basePath. DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR;
$filedir = str_replace($rootPath, '', $this->path);
$filedir = strtr($filedir, '\\', '/');
$filename = $file->getName();
$this->path .= (DIRECTORY_SEPARATOR. $filename) ;
$fileId = 1; // 文件序号, 用于判断是第几个文件
if (isset($_REQUEST['id']))
{
$fileId = (int) $_REQUEST['id'];
}
if (!is_dir($filedir))
{
mkdir($filedir, 0777,true);
}
$file->saveAs($this->path);
$_SESSION['temp_file'] = $this->path;
echo 'JS:('.$this->callbackJS.")('$fileId','$filename','$filedir','{$file->getName()}');";
$this->onAfterUpload(new CEvent(array('uploadedFile'=>&$file,'name'=>$filename,'path'=>$filedir)));
return $this->path;
}
}