upload.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. include "conn.php";
  3. checkLogin("");
  4. // upload demo for PHP
  5. // @requires xhEditor
  6. //
  7. // @author Yanis.Wang<yanis.wang@gmail.com>
  8. // @site http://xheditor.com/
  9. // @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
  10. //
  11. // @Version: 0.9.3 (build 100504)
  12. //
  13. // 注1:本程序仅为演示用,请您根据自己需求进行相应修改,或者重开发
  14. header('Content-Type: text/html; charset=utf-8');
  15. $inputname = 'filedata'; // 表单文件域name
  16. $attach_dir = 'u'; // 上传文件保存路径,结尾不要带/
  17. $dirtype = 2; // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
  18. $maxattachsize = 5242880; // 最大上传大小,默认是5M
  19. $upext = 'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,pdf'; // 上传扩展名
  20. $msgtype = 2; // 返回上传参数的格式:1,只返回url,2,返回参数数组
  21. $immediate = $_GET['immediate'] ?? ''; // 立即上传模式,仅为演示用
  22. $err = '';
  23. $msg = "''";
  24. // 检查文件上传
  25. if (!isset($_FILES[$inputname])) {
  26. $err = "无数据提交";
  27. } else {
  28. $file = $_FILES[$inputname];
  29. // 检查文件大小
  30. if ($file['size'] > $maxattachsize) {
  31. $err = "文件大小超过 " . $maxattachsize . "字节";
  32. } else {
  33. $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  34. // 检查文件类型
  35. if (!in_array($extension, explode(',', $upext))) {
  36. $err = "上传文件扩展名必需为:" . $upext;
  37. } else {
  38. // 确定子目录
  39. switch ($dirtype) {
  40. case 1:
  41. $attach_subdir = 'day_' . date('ymd');
  42. break;
  43. case 2:
  44. $attach_subdir = 'm' . date('ym');
  45. break;
  46. case 3:
  47. $attach_subdir = 'ext_' . $extension;
  48. break;
  49. }
  50. $upload_dir = $attach_dir . '/' . $attach_subdir . '/';
  51. // 创建目录
  52. if (!file_exists($upload_dir)) {
  53. mkdir($upload_dir, 0777, true);
  54. }
  55. // 生成随机文件名
  56. $filename = date('dHis') . sprintf('%02d', rand(0, 99)) . '.' . $extension;
  57. $target = $upload_dir . $filename;
  58. // 移动上传文件
  59. if (move_uploaded_file($file['tmp_name'], $target)) {
  60. $imgurl = $target;
  61. $target = jsonString('/system/' . $target);
  62. if ($msgtype == 1) {
  63. $msg = "'" . $target . "'";
  64. } else {
  65. $msg = "{'url':'" . $target . "','localname':'" . jsonString($file['name']) . "','id':'1'}";
  66. }
  67. } else {
  68. $err = "文件上传失败";
  69. }
  70. }
  71. }
  72. }
  73. $act = $_GET['act'] ?? '';
  74. if ($act == 's') {
  75. echo "<script>parent.document.getElementById('ProductImg').value='/System/" . $imgurl . "';location.href='uploadfile.php';</script>";
  76. } else {
  77. echo "{'err':'" . jsonString($err) . "','msg':" . $msg . "}";
  78. }
  79. function jsonString($str) {
  80. $str = str_replace("\\", "\\\\", $str);
  81. $str = str_replace("/", "\\/", $str);
  82. $str = str_replace("'", "\\'", $str);
  83. return $str;
  84. }
  85. function dateFormat($date, $format) {
  86. return date($format, strtotime($date));
  87. }
  88. ?>