upload_json.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace qiniu\upload;
  3. /****************************************************
  4. * NKeditor PHP
  5. * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
  6. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  7. * **************************************************
  8. * @author yangjian<yangjian102621@gmail.com>
  9. * 文件上传程序
  10. */
  11. error_reporting(0);
  12. use Qiniu\Auth;
  13. use Qiniu\Storage\UploadManager;
  14. require_once "vendor/autoload.php";
  15. require_once "../JsonResult.php";
  16. require_once "config.php";
  17. require_once "../functions.php";
  18. $fileType = trim($_GET['fileType']);
  19. if (empty($fileType)) {
  20. $fileType = "image";
  21. }
  22. // 构建鉴权对象
  23. $auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
  24. // 生成上传 Token
  25. $token = $auth->uploadToken(QINIU_TEST_BUCKET);
  26. // 要上传文件的本地路径
  27. //$filePath = './php-logo.png';
  28. $base64 = trim($_POST['base64']);
  29. if ($base64) {
  30. $data = $_POST['img_base64_data'];
  31. $filename = "{$fileType}-".time().".png";
  32. $res = base64Upload($data, $filename, $token);
  33. $json = new \JsonResult();
  34. if ($res) {
  35. $res = json_decode($res, true);
  36. $json->setCode(\JsonResult::CODE_SUCCESS);
  37. $json->setData(array('url' => QINIU_BUCKET_DOMAIN.$res['key']));
  38. } else {
  39. $json->setCode(\JsonResult::CODE_FAIL);
  40. $json->setMessage("上传涂鸦失败!");
  41. }
  42. $json->output();
  43. } else {
  44. $filePath = $_FILES['imgFile']['tmp_name'];
  45. $fileExt = getFileExt($_FILES['imgFile']['name']);
  46. // 返回结果
  47. $json = new \JsonResult();
  48. //定义允许上传的文件扩展名
  49. $extArr = array(
  50. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  51. 'flash' => array('swf', 'flv'),
  52. 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
  53. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
  54. );
  55. //最大文件大小 2MB
  56. $maxSize = 2*1024*1024;
  57. if (!in_array($fileExt, $extArr[$fileType])) {
  58. $json->setCode(\JsonResult::CODE_FAIL);
  59. $json->setMessage("非法的文件后缀名.");
  60. $json->output();
  61. }
  62. if (filesize($filePath) > $maxSize) {
  63. $json->setCode(\JsonResult::CODE_FAIL);
  64. $json->setMessage("文件大小超出限制 2MB.");
  65. $json->output();
  66. }
  67. // 上传到七牛后保存的文件名
  68. $key = $fileType . "-" . time() . mt_rand(1000, 9999) . ".{$fileExt}";
  69. // 初始化 UploadManager 对象并进行文件的上传。
  70. $uploadMgr = new UploadManager();
  71. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  72. list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
  73. if ($err !== null) {
  74. $json->setCode(\JsonResult::CODE_FAIL);
  75. $json->setMessage("上传失败.");
  76. } else {
  77. $json->setCode(\JsonResult::CODE_SUCCESS);
  78. $json->setMessage("上传成功.".$_POST['name']);
  79. $json->setData(array('url' => QINIU_BUCKET_DOMAIN . $ret['key']));
  80. }
  81. $json->output();
  82. }