FileManage.php 11 KB

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