uploadAction.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. class uploadClassAction extends apiAction
  3. {
  4. /**
  5. * 上传文件
  6. */
  7. public function upfileAction()
  8. {
  9. if(!$_FILES)exit('sorry!');
  10. $upimg = c('upfile');
  11. $maxsize= (int)$this->get('maxsize', $upimg->getmaxzhao());//上传最大M
  12. $uptypes= '*';
  13. $updir = $this->get('updir');
  14. if(isempt($updir)){
  15. $updir=date('Y-m');
  16. }else{
  17. $updir=str_replace(array(' ','.'),'', trim($updir));
  18. $updir=str_replace('{month}',date('Y-m'), $updir);
  19. $updir=str_replace('{Year}',date('Y'), $updir);
  20. $updir=str_replace(array('{','}'),'', $updir);
  21. $updir=str_replace(',','|', $updir);
  22. }
  23. $upimg->initupfile($uptypes, ''.UPDIR.'|'.$updir.'', $maxsize);
  24. $upses = $upimg->up('file');
  25. if(!is_array($upses))exit($upses);
  26. $arr = c('down')->uploadback($upses);
  27. $arr['autoup'] = (getconfig('qcloudCos_autoup') || getconfig('alioss_autoup')) ? 1 : 0; //是否上传其他平台
  28. $this->returnjson($arr);
  29. }
  30. /**
  31. * 这个是用来在线编辑文档上传的
  32. */
  33. public function upfilevbAction()
  34. {
  35. $fileid = (int)$this->get('fileid','0');
  36. if($fileid==0)exit('fileid=0');
  37. $data = $this->getpostdata();
  38. if(isempt($data))return '没有数据';
  39. $fileext= $this->get('fileext');
  40. $uptype = '|doc|docx|xls|xlsx|ppt|pptx|';
  41. if(!contain($uptype,'|'.$fileext.'|'))$fileext='doc';
  42. $fileobj = m('file');
  43. $frs = $fileobj->getone($fileid); //记录
  44. if(!$frs)exit('文件记录不存在了');
  45. $frs['oldfilepath'] = $frs['filepath'];
  46. $filename = $frs['filename'];
  47. if(!contain($filename, '.'.$fileext.'')){
  48. $filename = str_replace(array('.doc','.xls','.ppt'), '.'.$fileext.'', $filename);
  49. }
  50. $filepath = ''.UPDIR.'/'.date('Y-m').'/'.date('d_His').''.rand(10,99).'.'.$fileext.'';
  51. $this->rock->createtxt($filepath, base64_decode($data));
  52. $filesize = filesize($filepath);
  53. $filesizecn = $this->rock->formatsize($filesize);
  54. //更新文件
  55. $fileobj->update(array(
  56. 'filename' => $filename,
  57. 'filepath' => $filepath,
  58. 'filenum' => '',
  59. 'filesize' => $filesize,
  60. 'filesizecn' => $filesizecn,
  61. 'fileext' => $fileext,
  62. 'pdfpath' => '',
  63. ),$fileid);
  64. c('cache')->del('filetopdf'.$fileid.'');
  65. //【弃用】发队列自动上传到信呼文件平台
  66. if(getconfig('autoup_toxinhudoc')){
  67. //c('rockqueue')->sendfile($fileid);
  68. }
  69. //上传到腾讯存储
  70. if(getconfig('qcloudCos_autoup')){
  71. c('rockqueue')->sendfile($fileid);
  72. }
  73. //告诉上传人说编辑了他的附件
  74. $mknums = arrvalue($frs,'mknum');
  75. if(!isempt($mknums) && $frs['mid']>0){
  76. $mid = $frs['mid'];
  77. $mknumsa = explode('|', $mknums);
  78. $modenum = $mknumsa[0];
  79. if(isset($mknumsa[1]))$mid = $mknumsa[1];
  80. $flow = m('flow')->initflow($modenum, $mid, false);
  81. $ssid = $flow->addlog(array(
  82. 'name' => '在线编辑'
  83. ));
  84. $ufrs = $frs;
  85. $ufrs['filepath'] = $frs['oldfilepath'];
  86. unset($ufrs['oldfilepath']);
  87. unset($ufrs['id']);
  88. $ufrs['mtype'] = 'flow_log';
  89. $ufrs['mid'] = $ssid;
  90. $ufrs['mknum'] = ''.$modenum.'|'.$mid.'';
  91. $ufrs['filename'] = str_replace('.'.$ufrs['fileext'].'','(备份).'.$ufrs['fileext'].'', $ufrs['filename']);
  92. $fileobj->insert($ufrs); //记录原来的文件
  93. //不是我创建就告诉创建人
  94. if($this->adminid<>$frs['optid'])
  95. $flow->push($frs['optid'],'', ''.$this->adminname.'在线编辑文件“'.$frs['filename'].'”', '文件在线编辑');
  96. $flow->floweditoffice($frs, $ufrs);
  97. }else if($this->adminid<>$frs['optid']){ //不知道关联哪个模块
  98. $flow = m('flow')->initflow('word');
  99. $flow->push($frs['optid'],'文档', ''.$this->adminname.'在线编辑文件“'.$frs['filename'].'”', '文件在线编辑',0, array(
  100. 'wxurl' => ''
  101. ));
  102. }
  103. $frs['filesize'] = $filesize;
  104. $fkey = $this->createtempurl($frs);
  105. return 'ok,'.$fkey.'';
  106. }
  107. /**
  108. * 上传时初始化看是不是存在文件
  109. */
  110. public function initfileAction()
  111. {
  112. $filesize = c('check')->onlynumber($this->post('filesize'));
  113. $fileext = $this->post('fileext');
  114. $filename = $this->getvals('filename');
  115. $where = "`fileext`='$fileext' and `filesize`='$filesize'";
  116. if(!isempt($filename))$where.=" and `filename`='$filename'";
  117. $frs = m('file')->getone($where,'*','`id` desc');
  118. $bo = false;
  119. if($frs){
  120. $filepath = $frs['filepath'];
  121. if(!isempt($filepath) && file_exists($filepath))$bo=true;
  122. }
  123. if($bo){
  124. $this->showreturn(json_encode($frs));
  125. }else{
  126. $this->showreturn('','not found', 201);
  127. }
  128. }
  129. public function upfileappAction()
  130. {
  131. if(!$_FILES)$this->showreturn('', '禁止访问', 201);
  132. $upimg = c('upfile');
  133. $maxsize= (int)$this->get('maxsize', $upimg->getmaxzhao());//上传最大M
  134. $uptypes= '*';
  135. $upimg->initupfile($uptypes, ''.UPDIR.'|reimchat|'.date('Y-m').'', $maxsize);
  136. $upses = $upimg->up('file');
  137. if(!is_array($upses))$this->showreturn('', $upses, 202);
  138. $arr = c('down')->uploadback($upses);
  139. $this->showreturn($arr);
  140. }
  141. public function upcontAction()
  142. {
  143. $cont = $this->post('content');
  144. if(isempt($cont))exit('sorry not cont');
  145. $cont = str_replace(' ','', $cont);
  146. $cont = $this->rock->jm->base64decode($cont);
  147. $arr = c('down')->createimage($cont,'png','截图');
  148. $this->returnjson($arr);
  149. }
  150. public function getfileAjax()
  151. {
  152. $cont = '';
  153. $path = ''.UPDIR.'/uptxt'.$this->adminid.'.txt';
  154. if(file_exists($path)){
  155. @$cont = file_get_contents($path);
  156. }
  157. $data = array();
  158. if($cont!=''){
  159. $arr = json_decode($cont, true);
  160. $msg = $arr['msg'];
  161. $data = $arr['data'];
  162. @unlink($path);
  163. }else{
  164. $msg = 'sorry,not infor!';
  165. }
  166. $this->showreturn($data, $msg);
  167. }
  168. public function getfileAction()
  169. {
  170. $fileid = (int)$this->post('fileid',0);
  171. $rs = m('file')->getone($fileid);
  172. $this->showreturn($rs);
  173. }
  174. public function downAction()
  175. {
  176. $id = (int)$this->jm->gettoken('id');
  177. m('file')->show($id);
  178. }
  179. //记录预览记录
  180. public function logsAction()
  181. {
  182. $fileid = (int)$this->post('fileid',0);
  183. $type = (int)$this->post('type',0);
  184. m('file')->addlogs($fileid, $type);
  185. }
  186. /**
  187. * 发送编辑权限
  188. */
  189. public function rockofficeeditAction()
  190. {
  191. $fileid = (int)$this->get('id');
  192. $lx = (int)$this->get('lx');
  193. $frs = m('file')->getone($fileid);
  194. if(!$frs)return returnerror('文件不存在了');
  195. $filepath = $frs['filepath'];
  196. $filepathout = $frs['filepathout'];
  197. if(substr($filepath,0,4)!='http' && !file_exists($filepath)){
  198. if(isempt($filepathout)){
  199. return returnerror('文件不存在了1');
  200. }else{
  201. $filepath = $filepathout;
  202. }
  203. }
  204. $uptype = '|doc|docx|xls|xlsx|ppt|pptx|';
  205. if(!contain($uptype,'|'.$frs['fileext'].'|'))return returnerror('不是文档类型无法在线编辑');
  206. $filename = $frs['filename'];
  207. $utes = 'edit';
  208. if($lx==1){
  209. $filename = '(只读)'.$filename.'';
  210. $utes = 'yulan';
  211. }
  212. $arr = array();
  213. $arr[0] = URL;
  214. $arr[1] = $filename;
  215. $arr[2] = $this->createtempurl($frs);
  216. $arr[3] = $this->rock->gethttppath($filepath); //下载地址
  217. $arr[4] = $fileid;
  218. $arr[5] = $this->adminid;
  219. $arr[6] = $this->token;
  220. $arr[7] = $utes;
  221. $arr[8] = $frs['fileext'];
  222. $str = '';
  223. foreach($arr as $s1)$str.=','.$s1.'';
  224. return returnsuccess(substr($str,1));
  225. }
  226. /**
  227. * 获取预览和下载地址
  228. */
  229. public function fileinfoAction()
  230. {
  231. $fileid = (int)$this->get('id');
  232. $type = (int)$this->get('type'); //0预览,1下载,2编辑
  233. $ismobile = (int)$this->get('ismobile'); //是否手机端
  234. return $this->fileinfoShow($fileid, $type, $ismobile);
  235. }
  236. public function fileinfoShow($fileid, $type, $ismobile)
  237. {
  238. $fobj = m('file');
  239. $frs = $fobj->getone($fileid);
  240. $this->frs = $frs;
  241. if(!$frs)return returnerror('文件不存在了');
  242. $filenum= $frs['filenum'];
  243. $fileext = $frs['fileext'];
  244. $filename = $frs['filename'];
  245. $filepath = $frs['filepath'];
  246. $filepathout = arrvalue($frs, 'filepathout');
  247. $data = array();
  248. $loadyuan = false;
  249. $data['url'] = '';
  250. $data['fileext']= $fileext;
  251. //预览
  252. if($type==0){
  253. if(!$fobj->isview($fileext))
  254. return returnerror('此'.$fileext.'类型文件不支持在线预览');
  255. }
  256. //从文件上传中心最新
  257. if(!isempt($filenum)){
  258. $dbs = m('admin');
  259. $barr = c('rockfile')->getdata('upload','fileinfo', array(
  260. 'fnum' => $filenum,
  261. 'lx' => $type,
  262. 'sysuid' => $this->adminid,
  263. 'ismobile' => $ismobile,
  264. 'sysname' => $this->rock->jm->base64encode($this->adminname),
  265. 'sysface' => $this->rock->jm->base64encode($dbs->getface($dbs->getmou('face',$this->adminid))),
  266. ));
  267. if(!$barr['success']){
  268. return $barr;
  269. }else{
  270. $loadyuan = true;
  271. $da = $barr['data'];
  272. $data['url'] = $da['url'];
  273. if($da['isimg'] && $type==0)$data['url'] = $da['imgurl'];
  274. }
  275. }
  276. $this->loadyuan = $loadyuan;
  277. //存自己服务器的
  278. if(!$loadyuan){
  279. if(substr($filepath,0,4)!='http' && isempt($filepathout) && !file_exists($filepath))return returnerror('文件不存在了1');
  280. if(c('upfile')->isimg($fileext)){
  281. $data['url'] = m('admin')->getface($filepath);
  282. }
  283. //下载
  284. if($type==1){
  285. $url = 'api.php?m=upload&id='.$fileid.'&a=down';
  286. if($this->rock->web=='wxbro')$url.= '&adminid='.$this->adminid.'&token='.$this->admintoken.'';
  287. $data['url'] = $url;
  288. }
  289. //编辑
  290. if($type==2){
  291. if(getconfig('officebj')=='1'){
  292. $data['fileext']='rockedit';
  293. $data['url'] = 'index.php?m=public&a=fileedit&id='.$fileid.'';
  294. }else{
  295. if($ismobile==1)return returnerror('移动端不支持在线编辑');
  296. $data['fileext']='rockoffice';
  297. $data['url'] = $this->rock->gethttppath($filepath);
  298. }
  299. }
  300. }
  301. $data['filename'] = $filename;
  302. $url = arrvalue($data, 'url');
  303. if($url==''){
  304. $url = 'index.php?m=public&a=fileviewer&id='.$fileid.'';
  305. }
  306. //用本地插件编辑和预览
  307. if($data['fileext']=='rockoffice'){
  308. $utes = 'edit';
  309. if($type==0){
  310. $filename = '(只读)'.$filename.'';
  311. $utes = 'yulan';
  312. }
  313. $arr = array();
  314. $arr[0] = URL;
  315. $arr[1] = $filename;
  316. $arr[2] = $this->createtempurl($frs);
  317. $arr[3] = $data['url']; //下载地址
  318. $arr[4] = $fileid;
  319. $arr[5] = $this->adminid;
  320. $arr[6] = $this->token;
  321. $arr[7] = $utes;
  322. $arr[8] = $fileext;
  323. $str = '';
  324. foreach($arr as $s1)$str.=','.$s1.'';
  325. $url = substr($str, 1);
  326. }
  327. $data['url'] = $url;
  328. $data['type'] = $type;
  329. $data['id'] = $fileid;
  330. $data['isview'] = $fobj->isview($fileext); //是否可直接预览
  331. return returnsuccess($data);
  332. }
  333. //生成唯一文件名键值
  334. private function createtempurl($frs)
  335. {
  336. $str = ''.md5(URL).'_'.$frs['filesize'].'_'.$frs['id'].'.'.$frs['fileext'].'';
  337. return $str;
  338. }
  339. /**
  340. * app上获取下载地址
  341. */
  342. public function appgetfileAction()
  343. {
  344. $id = (int)$this->post('id',0);
  345. $barr = $this->fileinfoShow($id, 1, 1);
  346. if(!$barr['success'])return $barr;
  347. $frs = $this->frs;
  348. $frs['filetype'] = m('file')->getmime($frs['fileext']);
  349. $frs['downurl'] = $barr['data']['url'].'&cfrom=app';
  350. if(!$this->loadyuan){
  351. $frs['downurl']= '';
  352. if(substr($frs['filepath'],0,4)=='http'){
  353. $frs['downurl'] = $frs['filepath'];
  354. }else{
  355. if(!file_exists($frs['filepath']) && arrvalue($frs,'filepathout'))$frs['downurl'] = $frs['filepathout'];
  356. }
  357. }
  358. return returnsuccess($frs);
  359. }
  360. /**
  361. * 编辑时验证
  362. */
  363. public function sendeditAction()
  364. {
  365. $id = (int)$this->get('id',0);
  366. $otype = (int)$this->get('otype',0);
  367. return c('rockedit')->sendedit($id, $this->admintoken, $otype);
  368. }
  369. /**
  370. * 获取文件信息
  371. */
  372. public function afileinfoAction()
  373. {
  374. $allfid = c('check')->onlynumber($this->get('allfid'));
  375. $filearr= array();
  376. if($allfid){
  377. $fobj = m('file');
  378. $frows = $fobj->getall('`id` in('.$allfid.')','filename,id,filesizecn,fileext,optname,thumbpath,thumbplat');
  379. foreach($frows as $k1=>$rs1){
  380. $rs1['thumbpath'] = $fobj->getthumbpath($rs1);
  381. $filearr['f'.$rs1['id'].''] = $rs1;
  382. }
  383. }
  384. return $filearr;
  385. }
  386. /**
  387. * 获取文件(写入到内容里)
  388. */
  389. public function filedaoAction()
  390. {
  391. $allfid = c('check')->onlynumber($this->get('fileid'));
  392. $filearr= array();
  393. $str = '';
  394. if($allfid){
  395. $fobj = m('file');
  396. $frows = $fobj->getall('`id` in('.$allfid.')');
  397. $urla = getconfig('outurl', URL);
  398. foreach($frows as $k1=>$rs1){
  399. $str.='<br>';
  400. $url = ''.$urla.''.$rs1['filepath'].'';
  401. if($sst = arrvalue($rs1,'filepathout'))$url = $sst;
  402. $flx = $rs1['fileext'];
  403. if(!contain($fobj->fileall,','.$flx.','))$flx='wz';
  404. $str1 = '';
  405. $imurl = ''.URL.'web/images/fileicons/'.$flx.'.gif';
  406. $thumbpath = $fobj->getthumbpath($rs1);
  407. if($fobj->isimg($flx) && !isempt($thumbpath))$imurl = $thumbpath;
  408. $str.='<img src="'.$imurl.'" align="absmiddle" height=20 width=20> <a target="_blank" href="'.$url.'">'.$rs1['filename'].'</a>('.$rs1['filesizecn'].')';
  409. }
  410. }
  411. return $str;
  412. }
  413. /**
  414. * 获取模版文件
  415. */
  416. public function getmfileAction()
  417. {
  418. $data = array();
  419. $fenlei = $this->jm->base64decode($this->get('fenlei'));
  420. $fenlei = $this->rock->xssrepstr($this->rock->iconvsql($fenlei));
  421. $where = m('admin')->getjoinstr('a.`receid`', $this->adminid, 1);
  422. $sql = 'select a.`name`,a.`wtype`,b.`filepath`,b.`id` from `[Q]wordxie` a left join `[Q]file` b on a.`fileid`=b.`id` where a.`fenlei`=\''.$fenlei.'\' and a.`isgk`=1 and ('.$where.')';
  423. $rows = $this->db->getall($sql);
  424. foreach($rows as $k=>$rs){
  425. $data[] = array(
  426. 'value' => $rs['id'],
  427. 'name' => $rs['name'],
  428. 'subname' => $rs['wtype'],
  429. );
  430. }
  431. return $data;
  432. }
  433. public function getmfilvAction()
  434. {
  435. $fileid = (int)$this->get('fileid','0');
  436. $frs = m('file')->getone($fileid);
  437. if(!$frs)return returnerror('不存在');
  438. $lujing = $frs['filepathout'];
  439. if(isempt($lujing)){
  440. $lujing = $frs['filepath'];
  441. if(substr($lujing,0,4)!='http' && !file_exists($lujing))return returnerror('文件不存在了');
  442. }
  443. $fileext = $frs['fileext'];
  444. $fname = $this->jm->base64decode($this->get('fname'));
  445. $fname = (isempt($fname)) ? $frs['filename'] : ''.$fname.'.'.$fileext.'';
  446. $filepath = ''.UPDIR.'/'.date('Y-m').'/'.date('d').'_rocktpl'.rand(1000,9999).'_'.$fileid.'.'.$fileext.'';
  447. $this->rock->createtxt($filepath, file_get_contents($lujing));
  448. $uarr = array(
  449. 'filename' => $fname,
  450. 'fileext' => $fileext,
  451. 'filepath' => $filepath,
  452. 'filesize' => filesize($filepath),
  453. 'filesizecn' => $this->rock->formatsize(filesize($filepath)),
  454. 'optid' => $this->adminid,
  455. 'optname' => $this->adminname,
  456. 'adddt' => $this->rock->now,
  457. 'ip' => $this->rock->ip,
  458. 'web' => $this->rock->web,
  459. );
  460. $uarr['id'] = m('file')->insert($uarr);
  461. return returnsuccess($uarr);
  462. }
  463. /**
  464. * 文库选择文件
  465. */
  466. public function changedataAction()
  467. {
  468. $rows = array();
  469. $uptp = $this->get('uptp');
  470. $tsye = $this->get('tsye');
  471. $key = $this->get('key');
  472. $selv = (int)$this->get('selvalue','0');
  473. $where = '`optid`='.$this->adminid.'';
  474. if($selv==1){
  475. $str = m('admin')->getjoinstrs('`shateid`', $this->adminid, 1);
  476. $where = '`id` in(select `fileid` from `[Q]word` where `type`=0 '.$str.' )';
  477. }
  478. if($key){
  479. $key = $this->rock->xssrepstr($this->jm->base64decode($key));
  480. $where.=" and `filename` like '%".$key."%'";
  481. }
  482. if($uptp && $uptp!='*'){
  483. if($uptp=='image')$uptp='jpg,png,gif,jpeg';
  484. $uptp = str_replace(',', "','", $uptp);
  485. $where.=" and `fileext` in('".$uptp."')";
  486. }
  487. $db = m('file');
  488. $rows = $db->getall($where,'id,filename,filesizecn,fileext,thumbpath,filepath,filepathout','`id` desc limit 10');
  489. foreach($rows as $k=>$rs){
  490. $rows[$k]['value'] = $rs['id'];
  491. $rows[$k]['name'] = $rs['filename'];
  492. $rows[$k]['subname']= $rs['filesizecn'];
  493. $rows[$k]['xuanbool']= true;
  494. if($tsye=='img'){
  495. if(!isempt($rs['filepathout']))$rows[$k]['filepath'] = $rs['filepathout'];
  496. }else{
  497. unset($rows[$k]['filepath']);
  498. }
  499. unset($rows[$k]['filepathout']);
  500. if(!isempt($rs['thumbpath'])){
  501. $rows[$k]['iconsimg'] = $rs['thumbpath'];
  502. }else{
  503. $flx = $rs['fileext'];
  504. if(!contain($db->fileall,','.$flx.','))$flx='wz';
  505. $rows[$k]['iconsimg'] = 'web/images/fileicons/'.$flx.'.gif';
  506. }
  507. }
  508. $count = $db->rows($where);
  509. $selectdata[] = array('value'=>'','name'=>'选自己上传');
  510. $selectdata[] = array('value'=>'1','name'=>'共享给我的');
  511. return array(
  512. 'rows' => $rows,
  513. 'totalCount' => $count,
  514. 'selectdata' => $selectdata
  515. );
  516. }
  517. }
粤ICP备19079148号