Ei kuvausta
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.

DownloadFile.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\user\model;
  14. use think\Db;
  15. use think\Model;
  16. //下载文件
  17. class DownloadFile extends Model
  18. {
  19. protected function initialize()
  20. {
  21. parent::initialize();
  22. }
  23. //获取单条下载文章的所有文件
  24. public function getDownFile($aid, $field = '*')
  25. {
  26. $result = Db::name('DownloadFile')->field($field)
  27. ->where('aid', $aid)
  28. ->order('sort_order asc')
  29. ->select();
  30. foreach ($result as $key => $val) {
  31. if (!empty($val['file_url'])) {
  32. $result[$key]['file_url'] = handle_subdir_pic($val['file_url'], 'soft');
  33. }
  34. if (!isset($val['server_name'])) {
  35. $result[$key]['server_name'] = $result[$key]['file_name'];
  36. }
  37. }
  38. return $result;
  39. }
  40. //删除单条下载文章的所有文件
  41. public function delDownFile($aid = array())
  42. {
  43. if (!is_array($aid)) {
  44. $aid = array($aid);
  45. }
  46. $result = Db::name('DownloadFile')->where(array('aid'=>array('IN', $aid)))->delete();
  47. if ($result !== false) {
  48. Db::name('download_log')->where(array('aid'=>array('IN', $aid)))->delete();
  49. }
  50. return $result;
  51. }
  52. //保存下载文章的文件
  53. public function savefile($aid, $post = array())
  54. {
  55. // 拼装本地链接数据
  56. $data = array();
  57. $sort_order = 0;
  58. $fileupload = isset($post['fileupload']) ? $post['fileupload'] : array();
  59. if (!empty($fileupload)) {
  60. foreach($fileupload['file_url'] as $key => $val)
  61. {
  62. if($val == null || empty($val)) continue;
  63. $title = !empty($post['title']) ? $post['title'] : '';
  64. $file_size = isset($post['fileupload']['file_size'][$key]) ? $post['fileupload']['file_size'][$key] : 0;
  65. $file_mime = isset($post['fileupload']['file_mime'][$key]) ? $post['fileupload']['file_mime'][$key] : '';
  66. $uhash = isset($post['fileupload']['uhash'][$key]) ? $post['fileupload']['uhash'][$key] : md5($val);
  67. $md5file = isset($post['fileupload']['md5file'][$key]) ? $post['fileupload']['md5file'][$key] : md5($val);
  68. $file_name = isset($post['fileupload']['file_name'][$key]) ? $post['fileupload']['file_name'][$key] : '';
  69. $file_ext = isset($post['fileupload']['file_ext'][$key]) ? $post['fileupload']['file_ext'][$key] : '';
  70. $server_name = isset($post['fileupload']['server_name'][$key]) ? $post['fileupload']['server_name'][$key] : '';
  71. ++$sort_order;
  72. $data[] = array(
  73. 'aid' => $aid,
  74. 'title' => $title,
  75. 'file_url' => $val,
  76. 'extract_code' => '',
  77. 'file_size' => $file_size,
  78. 'file_ext' => $file_ext,
  79. 'file_name' => $file_name,
  80. 'file_mime' => $file_mime,
  81. 'uhash' => $uhash,
  82. 'md5file' => $md5file,
  83. 'server_name' => $server_name,
  84. 'is_remote' => 0,
  85. 'sort_order' => $sort_order,
  86. 'add_time' => getTime(),
  87. );
  88. }
  89. }
  90. // 拼装远程链接数据
  91. $data_new = array();
  92. if (!empty($post['remote_file'])) {
  93. foreach($post['remote_file'] as $kkk => $vvv)
  94. {
  95. if($vvv == null || empty($vvv)) continue;
  96. $server_name = !empty($post['server_name'][$kkk]) ? trim($post['server_name'][$kkk]) : '';
  97. $extract_code = !empty($post['extract_code'][$kkk]) ? trim($post['extract_code'][$kkk]) : '';
  98. ++$sort_order;
  99. $data_new[] = array(
  100. 'aid' => $aid,
  101. 'title' => $post['title'],
  102. 'file_url' => $vvv,
  103. 'extract_code' => $extract_code,
  104. 'file_size' => '0',
  105. 'file_ext' => '',
  106. 'file_name' => $server_name,
  107. 'file_mime' => '',
  108. 'uhash' => md5($vvv),
  109. 'md5file' => md5($vvv),
  110. 'server_name' => $server_name,
  111. 'is_remote' => 1,
  112. 'sort_order' => $sort_order,
  113. 'add_time' => getTime(),
  114. );
  115. }
  116. }
  117. $data_new_new = [];
  118. if (!empty($data) && !empty($data_new)) {
  119. $data_new_new = array_merge($data,$data_new);
  120. }else if (!empty($data)) {
  121. $data_new_new = $data;
  122. }else if (!empty($data_new)) {
  123. $data_new_new = $data_new;
  124. }
  125. $this->delDownFile($aid);
  126. if (!empty($data_new_new)) {
  127. Db::name('DownloadFile')->insertAll($data_new_new);
  128. }
  129. }
  130. }