import.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * 从老版本的 kindeditor 升级到 NKeditor,你可能需要把之前的文件初始化插入数据库。
  4. * 考虑到你可能更改了之前的老版本的文件上传代码,比如存储路径的风格,这里就只个给出一个参考思路:
  5. * 1. 遍历你的附件存储根目录, 并根据不同的文件类型 image, flash, file, media 分别创建4个数据库;
  6. * 2. 分别递归遍历这4个文件夹,把数据文件的 url 插入到各自的 SimpleDB 数据库.
  7. * ----------------------
  8. * 重建文件索引, 请使用命令行运行 php import.php
  9. * @author yangjian
  10. */
  11. error_reporting(0);
  12. require_once "db/SimpleDB.php";
  13. require_once "../functions.php";
  14. // 文件上传的根目录,请根据自己的实际情况修改
  15. $root = $basePath = dirname(dirname(__DIR__)) . "/uploads/";
  16. // 图片上传的根url,请根据实际项目修改
  17. $baseUrl = "/editor/nkeditor/uploads/";
  18. //如果数据库已经存在,则先删除
  19. $datadir = __DIR__.'/db/data';
  20. file_exists($datadir) && deldir($datadir);
  21. chdir($root);
  22. $dirs = glob("*");
  23. foreach ($dirs as $dir) {
  24. $db = new SimpleDB($dir);
  25. walkDir($root.$dir, $db, $dir);
  26. }
  27. tprint("数据导入完毕!");
  28. /**
  29. * 遍历目录,建立路径索引
  30. * @param $dir
  31. * @param SimpleDB $db
  32. * @param $fileType
  33. */
  34. function walkDir($dir, $db, $fileType) {
  35. $handler = opendir($dir);
  36. global $root;
  37. global $baseUrl;
  38. while (($filename = readdir($handler)) !== false) {
  39. if ($filename != '.' && $filename != '..') {
  40. $filePath = $dir.'/'.$filename;
  41. if (is_dir($filePath)) {
  42. walkDir($filePath, $db, $fileType);
  43. continue;
  44. }
  45. $filesize = filesize($filePath);
  46. //如果是图片则获取尺寸
  47. if ($fileType == "image") {
  48. $size = getimagesize($filePath);
  49. }
  50. $fileUrl = $baseUrl.str_replace('\\', '/', str_replace($root, '', $filePath));
  51. $data = [
  52. "thumbURL" => $fileUrl,
  53. "oriURL" => $fileUrl,
  54. "filesize" => $filesize,
  55. "width" => intval($size[0]),
  56. "height" => intval($size[1])
  57. ];
  58. $db->putLine($data);
  59. tprint("添加路径: {$fileUrl}");
  60. }
  61. }
  62. closedir($handler);
  63. }
  64. /**
  65. * 终端打印函数
  66. * @param $message
  67. */
  68. function tprint($message) {
  69. printf("\033[32m\033[1m{$message}\033[0m\n");
  70. }