You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

4 years ago
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class AttachmentModel extends BaseModel {
  9. Protected $autoCheckFields = false; //一定要关闭字段缓存,不然会报找不到表的错误
  10. //获取某个用户的当前已使用附件流量
  11. public function getUserFlow($uid){
  12. $month = Date("Y-m") ;
  13. $file_flow = D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->find() ;
  14. if($file_flow){
  15. return intval($file_flow['used']) ;
  16. }else{
  17. D("FileFlow")->add(array(
  18. "uid" => $uid ,
  19. "used" => 0 ,
  20. "date_month" => $month ,
  21. ));
  22. return 0 ;
  23. }
  24. }
  25. //记录某个用户流量
  26. public function recordUserFlow($uid , $file_size){
  27. $month = Date("Y-m") ;
  28. $used = $this->getUserFlow($uid) ;
  29. return D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->save(array(
  30. "used" => $used + intval($file_size)
  31. ));
  32. }
  33. public function deleteFile($file_id){
  34. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  35. $real_url = $file['real_url'] ;
  36. $array = explode("/Public/Uploads/", $real_url) ;
  37. $file_path = "../Public/Uploads/".$array[1] ;
  38. if (file_exists($file_path)) {
  39. @unlink($file_path);
  40. }
  41. D("UploadFile")->where(" file_id = '$file_id' ")->delete();
  42. return true ;
  43. }
  44. }