FileManage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\FileManageModel;
  11. use app\common\service\UploadService;
  12. use file\DirHelper;
  13. use file\FileHelper;
  14. use file\ImgHelper;
  15. use file\PathHelper;
  16. use file\ZipHelper;
  17. use network\FtpHelper;
  18. use think\Exception;
  19. use think\Log;
  20. class FileManage extends Permissions
  21. {
  22. public function index()
  23. {
  24. $relativePath = $this->request->param('relative_path', '', 'replaceDS,deleteEndDS');
  25. $this->assign('relative_path', $relativePath);
  26. if ($this->request->isAjax()) {
  27. $keyword = $this->request->param('keyword', '', 'trim');
  28. if (ifContain($keyword, DS)) {
  29. if (ifContain($keyword, FileManageModel::getRootDir())) {
  30. $keyword = str_replace(FileManageModel::getRootDir(), '', $keyword);
  31. } else {
  32. $keyword = deleteStartDS($keyword);
  33. }
  34. if (file_exists(FileManageModel::getRootDir() . $keyword)) {
  35. if (is_dir(FileManageModel::getRootDir() . $keyword)) {
  36. $relativePath2 = $keyword;
  37. } else {
  38. $relativePath2 = PathHelper::getParentDir($keyword);
  39. }
  40. if ($relativePath != $relativePath2) {
  41. $this->success($relativePath2, 'index');
  42. } else {
  43. $keyword = PathHelper::getFilename($keyword);
  44. }
  45. }
  46. }
  47. $data = FileManageModel::getFileList(trim($keyword), $relativePath);
  48. return array('code' => 0, 'data' => $data);
  49. } else {
  50. $this->assign('fullPath', appendEndDS(appendEndDS(FileManageModel::getRootDir()) . $relativePath));
  51. $arr = explode(DS, $relativePath);
  52. $parentPath = end($arr) == $relativePath ? "" : str_replace(DS . end($arr), '', $relativePath);
  53. $this->assign('parentPath', $parentPath);
  54. return $this->fetch();
  55. }
  56. }
  57. public function publish()
  58. {
  59. $rootpath = FileManageModel::getRootDir();
  60. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  61. $this->assign('relative_path', $relativePath);
  62. $post = $this->request->post();
  63. $fullname = $this->request->param('fullname');//文件名
  64. $name = $this->request->param('name');//新增模板名
  65. if ($fullname) {
  66. //修改操作
  67. $filePath = $rootpath . DS . $relativePath . DS . $fullname;
  68. if ($this->request->isPost()) {
  69. $getfilemtime = $this->request->param('filemtime');
  70. if ($getfilemtime != filemtime($filePath)) {
  71. $this->error('文件版本不正确,请刷新后重新提交');
  72. }
  73. try {
  74. FileHelper::save($filePath, $post['content']);
  75. } catch (\Exception $e) {
  76. $this->error('修改失败.' . $e->getMessage());
  77. }
  78. $this->success('修改成功', '', filemtime($filePath));
  79. } else {
  80. if (file_exists($filePath)) {
  81. if (is_dir($filePath)) {
  82. $this->error('不可以编辑目录');
  83. }
  84. if (ImgHelper::isImage($filePath)) {
  85. $this->error('不可以编辑图片');
  86. }
  87. $this->assign('fullname', $fullname);
  88. $this->assign('filemtime', filemtime($filePath));
  89. $this->assign('content', FileHelper::read($filePath));
  90. return $this->fetch();
  91. } else {
  92. $this->error('文件不存在');
  93. }
  94. }
  95. } else {
  96. //新增操作
  97. if ($this->request->isPost()) {
  98. $validate = new \think\Validate([
  99. ['name|模板名称', 'require'],
  100. ]);
  101. if (!$validate->check($post)) {
  102. $this->error('提交失败:' . $validate->getError());
  103. }
  104. $filePath = $rootpath . DS . $relativePath . DS . $name;
  105. if (file_exists($filePath)) {
  106. $this->error('该文件已存在,请修改名称');
  107. } else {
  108. try {
  109. FileHelper::save($filePath, $post['content']);
  110. } catch (\Exception $e) {
  111. $this->error('添加失败.' . $e->getMessage());
  112. }
  113. }
  114. $this->success('添加成功', 'index', ['relative_path' => $relativePath]);
  115. } else {
  116. return $this->fetch();
  117. }
  118. }
  119. }
  120. public function delete()
  121. {
  122. if ($this->request->isAjax()) {
  123. $rootpath = FileManageModel::getRootDir();
  124. $fullname = $this->request->param('fullname');
  125. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  126. $fullpath = $rootpath . DS . $relativePath . DS . $fullname;
  127. if (!file_exists($fullpath)) {
  128. $this->error('文件不存在');
  129. }
  130. if (is_dir($fullpath)) {
  131. DirHelper::delDir($fullpath);
  132. if (file_exists($fullpath)) {
  133. $this->error("删除失败,请检查目录权限");
  134. }
  135. $this->success('删除成功', 'index');
  136. } else {
  137. if (false == unlink($fullpath)) {
  138. $this->error('删除失败');
  139. } else {
  140. $this->success('删除成功', 'index');
  141. }
  142. }
  143. }
  144. }
  145. public function rename()
  146. {
  147. if ($this->request->isAjax()) {
  148. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  149. $oldname = $this->request->param('oldname');
  150. $newname = $this->request->param('newname');
  151. $rootpath = FileManageModel::getRootDir();
  152. if (file_exists($rootpath . DS . $newname)) {
  153. $this->error('文件已存在');
  154. }
  155. if (FileHelper::rename($rootpath . DS . $relativePath . DS . $oldname, $rootpath . DS . $relativePath . DS . $newname)) {
  156. $this->success('重命名成功', 'index');
  157. } else {
  158. $this->error('重命名失败');
  159. }
  160. }
  161. }
  162. public function upload()
  163. {
  164. //html目录
  165. $rootpath = FileManageModel::getRootDir();
  166. //相对路径
  167. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  168. (new UploadService())->upload2path(appendEndDS(appendEndDS($rootpath) . $relativePath));
  169. }
  170. public function createDir()
  171. {
  172. $name = $this->request->param('name');
  173. //根目录
  174. $rootpath = FileManageModel::getRootDir();
  175. //根目录下的相对路径
  176. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  177. if (true === DirHelper::makeDir($rootpath . DS . $relativePath . DS . $name)) {
  178. $this->success('执行成功');
  179. } else {
  180. $this->error('执行失败');
  181. }
  182. }
  183. public function unzip()
  184. {
  185. $post = $this->request->param();
  186. $file_list = $post['file_list'];
  187. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  188. $rootPath = FileManageModel::getRootDir();
  189. try {
  190. foreach ($file_list as $filename) {
  191. $source = $rootPath . DS . ($relativePath ? $relativePath . DS . $filename : $filename);
  192. $dest = $rootPath . DS . $relativePath;
  193. if (ifContain($source, '.zip')) {
  194. if (false === (new ZipHelper())->unzip_gbk($source, $dest)) {
  195. throw new \Exception('解压失败');
  196. }
  197. }
  198. }
  199. } catch (Exception $e) {
  200. $this->error($e->getMessage());
  201. }
  202. $this->success('执行成功');
  203. }
  204. /**
  205. * 上传到ftp ,要防止数据量过多
  206. */
  207. public function ftpCover()
  208. {
  209. $post = $this->request->param();
  210. $file_list = $post['file_list'];
  211. $relativePath = $this->request->param('relative_path', '', 'replaceDS');
  212. //
  213. $rootPath = FileManageModel::getRootDir();
  214. $config = \app\common\model\Webconfig::getFtpConfig();
  215. $ftpHost = $config['host']??'';
  216. if (empty($ftpHost)) {
  217. $this->error('请先配置ftp');
  218. }
  219. try {
  220. $ftp = new FtpHelper();
  221. $ftp->connect($ftpHost, $config['user']??'', $config['pwd']??'', $config['port']??21);
  222. //设置根目录
  223. if ($config['webRoot']??'') {
  224. $ftp->cd($config['webRoot']??'');
  225. }
  226. foreach ($file_list as $filename) {
  227. $upDownPath = $rootPath . DS . ($relativePath ? $relativePath . DS . $filename : $filename);
  228. if (is_file($upDownPath)) {
  229. Log::log($upDownPath);
  230. $ftp->mkdir($relativePath);//新建远程目录
  231. $ftp->upload($relativePath . DS . $filename, $upDownPath);
  232. } else {
  233. $this->ftpUpload($upDownPath, $rootPath, $ftp);
  234. }
  235. }
  236. } catch (Exception $e) {
  237. $this->error($e->getMessage());
  238. }
  239. $this->success('执行成功');
  240. }
  241. /**
  242. * 递归上传ftp
  243. * @param $dir [要上传的文件或目录的物理路径]
  244. * @param $rootPath [本地根目录]
  245. * @param $ftp FtpHelper
  246. */
  247. private function ftpUpload($dir, $rootPath, $ftp)
  248. {
  249. Log::log($dir);
  250. if (file_exists($dir)) {
  251. $ftpDir = replaceUrlDS(str_replace($rootPath, '', $dir));
  252. if ($ftpDir) {
  253. $ftp->mkdir($ftpDir);//新建远程目录
  254. }
  255. $mydir = dir($dir);
  256. while (false !== ($file = $mydir->read())) {
  257. if ($file != "." && $file != "..") {
  258. $path = $dir . DS . $file;
  259. if (is_dir($path)) {
  260. $this->ftpUpload($path, $rootPath, $ftp);
  261. } else {
  262. // if (time() - filemtime($path) <= 3600 * 24) {//修改时间是否超过24小时
  263. Log::log($path);
  264. $ftp->upload($ftpDir . DS . $file, $path);
  265. // }
  266. }
  267. }
  268. }
  269. $mydir->close();
  270. }
  271. }
  272. }