upload_json.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /****************************************************
  3. * NKeditor PHP
  4. * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
  5. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  6. * **************************************************
  7. * @author yangjian<yangjian102621@gmail.com>
  8. * 文件上传程序
  9. */
  10. error_reporting(0);
  11. require_once '../JsonResult.php';
  12. require_once '../functions.php';
  13. require_once "db/SimpleDB.php";
  14. $fileType = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
  15. //文件保存目录路径
  16. $basePath = BASE_PATH."{$fileType}/".UPLOAD_PREFIX;
  17. //文件保存目录URL
  18. $baseUrl = BASE_URL . "{$fileType}/".UPLOAD_PREFIX;
  19. //定义允许上传的文件扩展名
  20. $allowExtesions = array(
  21. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  22. 'flash' => array('swf', 'flv'),
  23. 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
  24. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
  25. );
  26. //最大文件大小 2MB
  27. $maxSize = 2*1024*1024;
  28. if (!file_exists($basePath)) {
  29. mkdirs($basePath);
  30. }
  31. //PHP上传失败
  32. if (!empty($_FILES['imgFile']['error'])) {
  33. switch($_FILES['imgFile']['error']){
  34. case '1':
  35. $error = '超过php.ini允许的大小。';
  36. break;
  37. case '2':
  38. $error = '超过表单允许的大小。';
  39. break;
  40. case '3':
  41. $error = '图片只有部分被上传。';
  42. break;
  43. case '4':
  44. $error = '请选择图片。';
  45. break;
  46. case '6':
  47. $error = '找不到临时目录。';
  48. break;
  49. case '7':
  50. $error = '写文件到硬盘出错。';
  51. break;
  52. case '8':
  53. $error = 'File upload stopped by extension。';
  54. break;
  55. case '999':
  56. default:
  57. $error = '未知错误。';
  58. }
  59. alert($error);
  60. }
  61. //base64 文件上传
  62. $base64 = trim($_POST['base64']);
  63. if ($base64) {
  64. $imgData = $_POST['img_base64_data'];
  65. $json = new JsonResult();
  66. if ($imgData && preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgData, $match)){
  67. $type = $match[2];
  68. $filename = date("YmdHis") . '_' . rand(10000, 99999) . '.png';
  69. if (file_put_contents($basePath.$filename, base64_decode(str_replace($match[1], '', $imgData)))){
  70. $json->setCode(JsonResult::CODE_SUCCESS);
  71. $json->setData(array('url' => $baseUrl.$filename));
  72. $json->output();
  73. }
  74. }
  75. $json->setCode(JsonResult::CODE_FAIL);
  76. $json->setMessage("涂鸦保存失败.");
  77. $json->output();
  78. }
  79. // input 文件上传
  80. if (empty($_FILES) == false) {
  81. //原文件名
  82. $filename = $_FILES['imgFile']['name'];
  83. //服务器上临时文件名
  84. $tmpName = $_FILES['imgFile']['tmp_name'];
  85. //文件大小
  86. $filesize = $_FILES['imgFile']['size'];
  87. //检查文件名
  88. if (!$filename) {
  89. alert("请选择文件。");
  90. }
  91. //检查目录
  92. if (@is_dir($basePath) === false) {
  93. alert("上传目录不存在。");
  94. }
  95. //检查目录写权限
  96. if (@is_writable($basePath) === false) {
  97. alert("上传目录没有写权限。");
  98. }
  99. //检查是否已上传
  100. if (@is_uploaded_file($tmpName) === false) {
  101. alert("上传失败。");
  102. }
  103. //检查文件大小
  104. if ($filesize > $maxSize) {
  105. alert("上传文件大小超过限制。");
  106. }
  107. //获得文件扩展名
  108. $extesion = getFileExt($filename);
  109. //检查扩展名
  110. if (in_array($extesion, $allowExtesions[$fileType]) === false) {
  111. alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $allowExtesions[$fileType]) . "格式。");
  112. }
  113. //新文件名
  114. $newFileName = genNewFilename($filename);
  115. //移动文件
  116. $filePath = $basePath . $newFileName;
  117. if (move_uploaded_file($tmpName, $filePath) === false) {
  118. alert("上传文件失败。");
  119. }
  120. @chmod($filePath, 0644);
  121. $fileUrl = $baseUrl . $newFileName;
  122. $json = new JsonResult(JsonResult::CODE_SUCCESS, "上传成功");
  123. $json->setData(array('url' => $fileUrl));
  124. //保存文件地址到数据库
  125. $db = new SimpleDB($fileType);
  126. //过滤掉非图片文件
  127. if ($fileType == "image") {
  128. $size = getimagesize($filePath);
  129. }
  130. $data = [
  131. "thumbURL" => $fileUrl,
  132. "oriURL" => $fileUrl,
  133. "filesize" => $filesize,
  134. "width" => intval($size[0]),
  135. "height" => intval($size[1])
  136. ];
  137. $db->putLine($data);
  138. $json->output();
  139. }
  140. function alert($msg) {
  141. $json = new JsonResult(JsonResult::CODE_FAIL, $msg);
  142. $json->output();
  143. }