123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- include "conn.php";
- checkLogin("");
- /**
- * upload demo for PHP
- * @requires xhEditor
- *
- * @author Original ASP version by Yanis.Wang<yanis.wang@gmail.com>
- * @author PHP version modified
- * @site http://xheditor.com/
- * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
- *
- * @Version: 0.9.3 (build 100504)
- *
- * 注1:本程序仅为演示用,请您根据自己需求进行相应修改,或者重开发
- */
- header('Content-Type: application/json; charset=UTF-8');
- // 配置参数
- $inputname = 'filedata'; // 表单文件域name
- $attachdir = 'u'; // 上传文件保存路径,结尾不要带/
- $dirtype = 2; // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录
- $maxattachsize = 5242880; // 最大上传大小,默认是5M
- $upext = 'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,pdf'; // 上传扩展名
- $msgtype = 2; // 返回上传参数的格式:1,只返回url,2,返回参数数组
- $immediate = $_GET['immediate'] ?? ''; // 立即上传模式,仅为演示用
- $id = $_GET['id'] ?? '';
- $err = '';
- $msg = "''";
- // 检查文件上传
- if (!isset($_FILES[$inputname])) {
- $err = "无数据提交";
- } else if ($_FILES[$inputname]['error'] > 0) {
- switch($_FILES[$inputname]['error']) {
- case 1:
- case 2:
- $err = "文件大小超过 " . $maxattachsize . "字节";
- break;
- case 3:
- $err = "文件只有部分被上传";
- break;
- case 4:
- $err = "没有文件被上传";
- break;
- case 6:
- $err = "找不到临时文件夹";
- break;
- case 7:
- $err = "文件写入失败";
- break;
- default:
- $err = "未知上传错误";
- }
- } else if ($_FILES[$inputname]['size'] > $maxattachsize) {
- $err = "文件大小超过 " . $maxattachsize . "字节";
- } else {
- $filename = $_FILES[$inputname]['name'];
- $tmpfile = $_FILES[$inputname]['tmp_name'];
- $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
-
- // 检查文件类型
- $allowed_types = explode(',', $upext);
- if (!in_array($extension, $allowed_types)) {
- $err = "上传文件扩展名必需为:" . $upext;
- } else {
- // 确定存储目录
- switch($dirtype) {
- case 1:
- $attach_subdir = "day_" . date('ymd');
- break;
- case 2:
- $attach_subdir = "m" . date('ym');
- break;
- case 3:
- $attach_subdir = "ext_" . $extension;
- break;
- }
-
- $attach_dir = $attachdir . '/' . $attach_subdir . '/';
-
- // 创建目录
- if (!file_exists($attach_dir)) {
- mkdir($attach_dir, 0777, true);
- }
-
- // 生成随机文件名
- $newfilename = date('dHis') . sprintf("%02d", rand(0, 99)) . '.' . $extension;
- $target = $attach_dir . $newfilename;
-
- // 移动文件
- if (move_uploaded_file($tmpfile, $target)) {
- $imgurl = $target;
- if ($immediate == '1') {
- $target = '!' . $target;
- }
-
- if ($msgtype == 1) {
- $msg = "'" . jsonString($target) . "'";
- } else {
- $msg = "{" .
- "'url':'" . jsonString($target) . "'," .
- "'localname':'" . jsonString($filename) . "'," .
- "'id':'1'" .
- "}";
- }
- } else {
- $err = "文件上传失败";
- }
- }
- }
- // 输出结果
- $act = $_GET['act'] ?? '';
- if ($act == 's') {
- echo "<script>parent.document.getElementById('" . $id . "').value='/system/" . $imgurl . "';location.href='imgUpload.php';</script>";
- } else {
- echo json_encode([
- 'err' => jsonString($err),
- 'msg' => $msg
- ]);
- }
- /**
- * JSON字符串转义
- */
- function jsonString($str) {
- return str_replace(
- ['\\', '/', "'"],
- ['\\\\', '\\/', "\\'"],
- $str
- );
- }
|