upload_json.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * KindEditor PHP
  4. *
  5. * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
  6. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  7. *
  8. */
  9. require_once 'JSON.php';
  10. require_once '../../../config/config.php';
  11. //文件保存目录路径
  12. $save_path = '../../images/upload/'; //这个需要根据需要做调整
  13. //文件保存目录URL
  14. $save_url = SITE_URL.'static/images/upload/';//这个需要根据需要做调整
  15. //定义允许上传的文件扩展名
  16. $ext_arr = array(
  17. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  18. 'flash' => array('swf', 'flv'),
  19. 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
  20. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
  21. );
  22. //最大文件大小
  23. $max_size = 1000000;
  24. $save_path = realpath($save_path) . '/';
  25. //有上传文件时
  26. if (empty($_FILES) === false) {
  27. //原文件名
  28. $file_name = $_FILES['imgFile']['name'];
  29. //服务器上临时文件名
  30. $tmp_name = $_FILES['imgFile']['tmp_name'];
  31. //文件大小
  32. $file_size = $_FILES['imgFile']['size'];
  33. //检查文件名
  34. if (!$file_name) {
  35. alert("请选择文件。");
  36. }
  37. //检查目录
  38. if (@is_dir($save_path) === false) {
  39. alert("上传目录不存在。");
  40. }
  41. //检查目录写权限
  42. if (@is_writable($save_path) === false) {
  43. alert("上传目录没有写权限。");
  44. }
  45. //检查是否已上传
  46. if (@is_uploaded_file($tmp_name) === false) {
  47. alert("临时文件可能不是上传文件。");
  48. }
  49. //检查文件大小
  50. if ($file_size > $max_size) {
  51. alert("上传文件大小超过限制。");
  52. }
  53. //检查目录名
  54. $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
  55. if (empty($ext_arr[$dir_name])) {
  56. alert("目录名不正确。");
  57. }
  58. //获得文件扩展名
  59. $temp_arr = explode(".", $file_name);
  60. $file_ext = array_pop($temp_arr);
  61. $file_ext = trim($file_ext);
  62. $file_ext = strtolower($file_ext);
  63. //检查扩展名
  64. if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
  65. alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
  66. }
  67. //创建文件夹
  68. if ($dir_name !== '') {
  69. $save_path .= $dir_name . "/";
  70. $save_url .= $dir_name . "/";
  71. if (!file_exists($save_path)) {
  72. mkdir($save_path);
  73. }
  74. }
  75. $ymd = date("Ymd");
  76. $save_path .= $ymd . "/";
  77. $save_url .=$ymd . "/";
  78. if (!file_exists($save_path)) {
  79. mkdir($save_path);
  80. }
  81. //新文件名
  82. $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
  83. //移动文件
  84. $file_path = $save_path . $new_file_name;
  85. if (move_uploaded_file($tmp_name, $file_path) === false) {
  86. alert("上传文件失败。");
  87. }
  88. @chmod($file_path, 0644);
  89. $file_url = $save_url . $new_file_name;
  90. header('Content-type: text/html; charset=UTF-8');
  91. $json = new Services_JSON();
  92. echo $json->encode(array('error' => 0, 'url' => $file_url));
  93. exit;
  94. }
  95. function alert($msg){
  96. header('Content-type: text/html; charset=UTF-8');
  97. $json = new Services_JSON();
  98. echo $json->encode(array('error' => 1, 'message' => $msg));
  99. exit;
  100. }
  101. ?>