imgUploadFile.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. include "conn.php";
  3. checkLogin("");
  4. /**
  5. * upload demo for PHP
  6. * @requires xhEditor
  7. *
  8. * @author Original ASP version by Yanis.Wang<yanis.wang@gmail.com>
  9. * @author PHP version modified
  10. * @site http://xheditor.com/
  11. * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
  12. *
  13. * @Version: 0.9.3 (build 100504)
  14. *
  15. * 注1:本程序仅为演示用,请您根据自己需求进行相应修改,或者重开发
  16. */
  17. header('Content-Type: application/json; charset=UTF-8');
  18. // 配置参数
  19. $inputname = 'filedata'; // 表单文件域name
  20. $attachdir = 'u'; // 上传文件保存路径,结尾不要带/
  21. $dirtype = 2; // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录
  22. $maxattachsize = 5242880; // 最大上传大小,默认是5M
  23. $upext = 'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,pdf'; // 上传扩展名
  24. $msgtype = 2; // 返回上传参数的格式:1,只返回url,2,返回参数数组
  25. $immediate = $_GET['immediate'] ?? ''; // 立即上传模式,仅为演示用
  26. $id = $_GET['id'] ?? '';
  27. $err = '';
  28. $msg = "''";
  29. // 检查文件上传
  30. if (!isset($_FILES[$inputname])) {
  31. $err = "无数据提交";
  32. } else if ($_FILES[$inputname]['error'] > 0) {
  33. switch($_FILES[$inputname]['error']) {
  34. case 1:
  35. case 2:
  36. $err = "文件大小超过 " . $maxattachsize . "字节";
  37. break;
  38. case 3:
  39. $err = "文件只有部分被上传";
  40. break;
  41. case 4:
  42. $err = "没有文件被上传";
  43. break;
  44. case 6:
  45. $err = "找不到临时文件夹";
  46. break;
  47. case 7:
  48. $err = "文件写入失败";
  49. break;
  50. default:
  51. $err = "未知上传错误";
  52. }
  53. } else if ($_FILES[$inputname]['size'] > $maxattachsize) {
  54. $err = "文件大小超过 " . $maxattachsize . "字节";
  55. } else {
  56. $filename = $_FILES[$inputname]['name'];
  57. $tmpfile = $_FILES[$inputname]['tmp_name'];
  58. $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  59. // 检查文件类型
  60. $allowed_types = explode(',', $upext);
  61. if (!in_array($extension, $allowed_types)) {
  62. $err = "上传文件扩展名必需为:" . $upext;
  63. } else {
  64. // 确定存储目录
  65. switch($dirtype) {
  66. case 1:
  67. $attach_subdir = "day_" . date('ymd');
  68. break;
  69. case 2:
  70. $attach_subdir = "m" . date('ym');
  71. break;
  72. case 3:
  73. $attach_subdir = "ext_" . $extension;
  74. break;
  75. }
  76. $attach_dir = $attachdir . '/' . $attach_subdir . '/';
  77. // 创建目录
  78. if (!file_exists($attach_dir)) {
  79. mkdir($attach_dir, 0777, true);
  80. }
  81. // 生成随机文件名
  82. $newfilename = date('dHis') . sprintf("%02d", rand(0, 99)) . '.' . $extension;
  83. $target = $attach_dir . $newfilename;
  84. // 移动文件
  85. if (move_uploaded_file($tmpfile, $target)) {
  86. $imgurl = $target;
  87. if ($immediate == '1') {
  88. $target = '!' . $target;
  89. }
  90. if ($msgtype == 1) {
  91. $msg = "'" . jsonString($target) . "'";
  92. } else {
  93. $msg = "{" .
  94. "'url':'" . jsonString($target) . "'," .
  95. "'localname':'" . jsonString($filename) . "'," .
  96. "'id':'1'" .
  97. "}";
  98. }
  99. } else {
  100. $err = "文件上传失败";
  101. }
  102. }
  103. }
  104. // 输出结果
  105. $act = $_GET['act'] ?? '';
  106. if ($act == 's') {
  107. echo "<script>parent.document.getElementById('" . $id . "').value='/system/" . $imgurl . "';location.href='imgUpload.php';</script>";
  108. } else {
  109. echo json_encode([
  110. 'err' => jsonString($err),
  111. 'msg' => $msg
  112. ]);
  113. }
  114. /**
  115. * JSON字符串转义
  116. */
  117. function jsonString($str) {
  118. return str_replace(
  119. ['\\', '/', "'"],
  120. ['\\\\', '\\/', "\\'"],
  121. $str
  122. );
  123. }