audioupload.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/6 0006
  6. * Time: 17:30
  7. */
  8. header('Access-Control-Allow-Origin:*');//允许跨域
  9. define('IN_WEB', true);
  10. $sid = 1;
  11. $lid = 1;
  12. $str = $_REQUEST['param'];
  13. $sign = substr($str,0,1).substr($str,2,strlen($str));
  14. $sign = substr($sign,0,floor((strlen($sign)-16)/2)-1).substr($sign,floor((strlen($sign)-16)/2)+15,strlen($sign));
  15. $sign = base64_decode($sign);
  16. $sign = trim($sign,'&');
  17. $sign = explode('&',$sign);
  18. foreach ($sign as $row){
  19. $temp = explode('=',$row,2);
  20. $_POST[$temp[0]] = $temp[1];
  21. }
  22. include_once dirname(__FILE__) . "/../com.php";
  23. $uid = intval($_POST['uid']);
  24. $toUid = intval($_POST['toUid']);
  25. $token = $_POST['saToken'];
  26. $file = $_POST['audio'];
  27. if(empty($uid) || empty($token) || empty($file)){
  28. return response(-6, 'param error');
  29. }
  30. $redisToken = oo::commonOprRedis('common')->get(okeys::UserToken($uid));
  31. if(empty($token) || $token != $redisToken ){
  32. return response(-5, 'token error');
  33. }
  34. if(empty($toUid)){
  35. $audioDir = 'storage/World/';
  36. }else{
  37. $audioDir = 'storage/'.$uid.'_'.$toUid.'/';
  38. }
  39. $uploadPath = getFolder('../'.$audioDir);
  40. if(!$uploadPath) {
  41. return response(-1, '存放文件图片文件夹创建失败');
  42. }
  43. $fileName = getName();
  44. $newFile = $uploadPath.$fileName;
  45. if(!file_put_contents($newFile, base64_decode($file))) {
  46. return response(-3, '文件出错');
  47. }
  48. amrTomp3($audioDir,$fileName);
  49. $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://": "http://";
  50. $path = $protocol.$_SERVER['SERVER_NAME'].'/'.$audioDir.str_replace('.amr','.mp3',$fileName);
  51. die(response(1, '文件上传成功', ['audioPath' => $path]));
  52. //---------end----------
  53. /**
  54. * 自动创建存储文件夹
  55. * @return string
  56. */
  57. function getFolder($pathStr)
  58. {
  59. if ( !is_dir( $pathStr ) ) {
  60. if ( !mkdir( $pathStr , 0777 , true ) ) {
  61. return false;
  62. }
  63. }
  64. return $pathStr;
  65. }
  66. /**
  67. * 重命名文件
  68. * @return string
  69. */
  70. function getName()
  71. {
  72. $ext = '.amr';
  73. return $fileName = time().uniqid().'_'.rand(1, 999).$ext;
  74. }
  75. function amrTomp3($path , $amr){
  76. $base = WWWROOT;
  77. $amrPath = $base.$path.$amr;
  78. $mp3Path = $base.$path.str_replace('.amr','.mp3',$amr);
  79. $command = "ffmpeg -i $amrPath $mp3Path";
  80. exec($command,$error);
  81. unlink($amrPath);
  82. }
  83. /**
  84. * 统一输出返回json内容
  85. *
  86. * @access public
  87. * @date 2016-08-16
  88. * @param int $ret 状态码,0成功
  89. * @param string $msg 错误信息
  90. * @param array $data 返回数据内容
  91. * @return json;
  92. */
  93. function response($ret = 0, $msg = '', array $data = array())
  94. {
  95. $result = [
  96. 'code' => $ret,
  97. 'msg' => $msg,
  98. 'data' => $data,
  99. ];
  100. die(json_encode($result));
  101. }