action_upload.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * 上传附件和上传视频
  4. * User: Jinqn
  5. * Date: 14-04-09
  6. * Time: 上午10:17
  7. */
  8. include "Uploader.class.php";
  9. include "upyun.class.php";
  10. include "upyun.config.php";
  11. /* 上传配置 */
  12. $base64 = "upload";
  13. switch (htmlspecialchars($_GET['action'])) {
  14. case 'uploadimage':
  15. $config = array(
  16. "pathFormat" => $CONFIG['imagePathFormat'],
  17. "maxSize" => $CONFIG['imageMaxSize'],
  18. "allowFiles" => $CONFIG['imageAllowFiles']
  19. );
  20. $fieldName = $CONFIG['imageFieldName'];
  21. break;
  22. case 'uploadscrawl':
  23. $config = array(
  24. "pathFormat" => $CONFIG['scrawlPathFormat'],
  25. "maxSize" => $CONFIG['scrawlMaxSize'],
  26. "allowFiles" => $CONFIG['scrawlAllowFiles'],
  27. "oriName" => "scrawl.png"
  28. );
  29. $fieldName = $CONFIG['scrawlFieldName'];
  30. $base64 = "base64";
  31. break;
  32. case 'uploadvideo':
  33. $config = array(
  34. "pathFormat" => $CONFIG['videoPathFormat'],
  35. "maxSize" => $CONFIG['videoMaxSize'],
  36. "allowFiles" => $CONFIG['videoAllowFiles']
  37. );
  38. $fieldName = $CONFIG['videoFieldName'];
  39. break;
  40. case 'uploadfile':
  41. default:
  42. $config = array(
  43. "pathFormat" => $CONFIG['filePathFormat'],
  44. "maxSize" => $CONFIG['fileMaxSize'],
  45. "allowFiles" => $CONFIG['fileAllowFiles']
  46. );
  47. $fieldName = $CONFIG['fileFieldName'];
  48. break;
  49. }
  50. /* 生成上传实例对象并完成上传 */
  51. $up = new Uploader($fieldName, $config, $base64);
  52. $info = $up->getFileInfo();
  53. // 将文件同步存储到又拍云
  54. $upyun = new UpYun($servename, $username, $password);
  55. try {
  56. $uri = strstr($info["url"], "upload");
  57. $info['url'] = '/'.$uri;
  58. if (file_exists($uri)) {
  59. $opts = array(
  60. UpYun::CONTENT_MD5 => md5(file_get_contents($uri))
  61. );
  62. $fh = fopen($uri, "rb");
  63. $rsp = $upyun->writeFile('/'.$uri, $fh, True, $opts);
  64. fclose($fh);
  65. unlink($uri);
  66. }
  67. else {
  68. $log = date("Y-m-d H:m:s") . " 文件不存在,请检查目录是否正确。" . "\r\n";
  69. $log_file = "log.txt";
  70. $handle = fopen($log_file, "a");
  71. fwrite($handle, $log);
  72. fclose($handle);
  73. exit;
  74. }
  75. }
  76. catch(Exception $e) {
  77. $log = date("Y-m-d H:m:s") . " " . $e->getCode() . " " . $e->getMessage() . "\r\n";
  78. $log_file = "log.txt";
  79. $handle = fopen($log_file, "a");
  80. fwrite($handle, $log);
  81. fclose($handle);
  82. exit;
  83. }
  84. /**
  85. * 得到上传文件所对应的各个参数,数组结构
  86. * array(
  87. * "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
  88. * "url" => "", //返回的地址
  89. * "title" => "", //新文件名
  90. * "original" => "", //原始文件名
  91. * "type" => "" //文件类型
  92. * "size" => "", //文件大小
  93. * )
  94. */
  95. /* 返回数据 */
  96. return json_encode($info);