Upload.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Upload extends CI_Controller {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->helper('string');
  6. $this->load->database();
  7. $this->load->model('User');
  8. $this->User->Login();
  9. }
  10. public function index(){
  11. $cid = (int)$this->input->get('cid');
  12. $data['uploadsave'] = site_url('upload/save/'.$cid);
  13. $data['vlist'] = $this->db->query("select * from ".CS_SqlPrefix."class order by id asc")->result();
  14. $data['cid'] = $cid;
  15. $this->load->view('upload.html',$data);
  16. }
  17. public function save($cid=0){
  18. $cid = (int)$cid;
  19. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  20. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  21. header("Cache-Control: no-store, no-cache, must-revalidate");
  22. header("Cache-Control: post-check=0, pre-check=0", false);
  23. header("Pragma: no-cache");
  24. set_time_limit(0);
  25. if(substr(Video_Path,0,2) == './'){
  26. $Video_Path = FCPATH.substr(Video_Path,2);
  27. }else{
  28. $Video_Path = Video_Path;
  29. }
  30. $targetDir = $Video_Path.'/temp/'.date('Ymd').'/';
  31. $uploadDir = $Video_Path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  32. $uploadStr = date('Y').'/'.date('m').'/'.date('d').'/';
  33. //判断MD5秒传
  34. $filemd5 = $this->input->post('md5',true);
  35. $filename = $this->input->post('filename',true);
  36. if(!empty($filemd5) && !empty($filename)){
  37. if(!preg_match("/[0-9A-Za-z]/",$filemd5)){
  38. getjson('MD5不合法');
  39. }elseif(Video_SrcOn == 1){
  40. //判断MD5是否存在
  41. $row = $this->db->query("select * from ".CS_SqlPrefix."video where md5='".$filemd5."'")->row();
  42. if($row){
  43. $file_ext = strtolower(trim(substr(strrchr($filename, '.'), 1)));
  44. $filename = safe_replace(str_replace('.'.$file_ext,'',$filename));
  45. //判断歌曲标题是否存在
  46. $res = $this->db->query("select id from ".CS_SqlPrefix."video where name='".$filename."'")->row();
  47. if($res) getjson("yes");
  48. //复制文件
  49. $newname = date('YmdHis').time().'.'.$file_ext;
  50. if(copy($row->path, $uploadDir.$newname)){
  51. //video
  52. $data['cid'] = $cid;
  53. $data['name'] = $filename;
  54. $data['path'] = $uploadDir.$newname;
  55. $data['md5'] = $filemd5;
  56. $data['duration'] = $row->duration;
  57. $data['size'] = $row->size;
  58. $data['addtime'] = time();
  59. $this->db->insert("video",$data);
  60. $insert_id = $this->db->insert_id();
  61. //M3U8保存目录
  62. $m3u8_dir = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  63. //路径
  64. $time = time();
  65. $m3u8path = m3u8_dir($insert_id,$time);
  66. $jpgpath = m3u8_dir($insert_id,$time,'jpg');
  67. $gifpath = m3u8_dir($insert_id,$time,'gif');
  68. $this->db->update('video',array('m3u8'=>$m3u8path,'pic'=>$jpgpath),array('id'=>$insert_id));
  69. //queue
  70. $queue['xu'] = 1;
  71. $queue['vid'] = $insert_id;
  72. $queue['path'] = $data['path'];
  73. $queue['jpg'] = $m3u8_dir.$jpgpath;
  74. $queue['gif'] = $m3u8_dir.$gifpath;
  75. $queue['m3u8'] = $m3u8_dir.$m3u8path;
  76. $queue['duration'] = $row->duration;
  77. $this->db->insert('queue',$queue);
  78. //多码率
  79. if(Mu_Type == 1){
  80. $ext = end(explode('/', $m3u8path));
  81. $queue['xu'] = 2;
  82. $queue['m3u8'] = $m3u8_dir.str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8path);
  83. $this->db->insert('queue',$queue);
  84. }
  85. //返回数据
  86. $ok['name'] = $filename;
  87. $ok['m3u8'] = m3u8_link($m3u8path);
  88. $ok['pic'] = m3u8_link($jpgpath,'jpg');
  89. $ok['purl'] = 'http://'.Web_Url.links('play/'.$insert_id);
  90. $ok['size'] = formatsize($row->size);
  91. $ok['sec'] = formattime($row->duration,1);
  92. $ok['msg'] = 'ok';
  93. getjson($ok,0,1);
  94. }
  95. }
  96. }
  97. getjson('no');
  98. }
  99. //定义允许上传的文件扩展名
  100. $ext_arr = array_filter(explode('|', Up_Ext));
  101. //防止外部跨站提交
  102. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  103. getjson('上传方式不正确');
  104. }
  105. //上传出错
  106. if (!empty($_REQUEST[ 'debug' ]) ) {
  107. $random = rand(0, intval($_REQUEST[ 'debug' ]) );
  108. if ( $random === 0 ) {
  109. header("HTTP/1.0 500 Internal Server Error"); exit;
  110. }
  111. }
  112. //创建目录
  113. if (!file_exists($targetDir)) {
  114. creatdir($targetDir);
  115. }
  116. //创建目录
  117. if (!file_exists($uploadDir)) {
  118. creatdir($uploadDir);
  119. }
  120. $file_name = $_FILES['video']['name'];
  121. //原文件名
  122. if(!isset($_FILES['video']['name'])){
  123. $file_name = rand(111111,999999).time().'.mp4';
  124. }
  125. //文件后缀
  126. $file_ext = strtolower(trim(substr(strrchr($file_name, '.'), 1)));
  127. $newname = iconv("UTF-8","GBK//IGNORE",$file_name);
  128. $video_name = safe_replace(str_replace('.'.$file_ext, '', $file_name));
  129. //判断是否存在
  130. $res = $this->db->query("select id from ".CS_SqlPrefix."video where name='".$video_name."'")->row();
  131. if($res){
  132. getjson("视频已经存在");
  133. }
  134. //判断文件后缀
  135. if(in_array($file_ext, $ext_arr) == false) {
  136. getjson("不支持的格式");
  137. }
  138. //根据文件名和会员ID生成一个唯一MD5
  139. $fileName = md5($file_name).'.'.$file_ext;
  140. $oldName = $fileName;
  141. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  142. //分片ID
  143. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  144. //分片总数量
  145. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
  146. //打开临时文件
  147. if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
  148. getjson('Failed to open output stream.');
  149. }
  150. if (!empty($_FILES)) {
  151. if ($_FILES["video"]["error"] || !is_uploaded_file($_FILES["video"]["tmp_name"])) {
  152. getjson('Failed to move uploaded file.');
  153. }
  154. //读取二进制输入流并将其附加到临时文件
  155. if (!$in = @fopen($_FILES["video"]["tmp_name"], "rb")) {
  156. getjson('Failed to open input stream.');
  157. }
  158. } else {
  159. if (!$in = @fopen("php://input", "rb")) {
  160. getjson('Failed to open input stream.');
  161. }
  162. }
  163. while ($buff = fread($in, 4096)) {
  164. fwrite($out, $buff);
  165. }
  166. @fclose($out);
  167. @fclose($in);
  168. rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
  169. $index = 0;
  170. $done = true;
  171. for( $index = 0; $index < $chunks; $index++ ) {
  172. if (!file_exists("{$filePath}_{$index}.part") ) {
  173. $done = false;
  174. break;
  175. }
  176. }
  177. if($done){
  178. $pathInfo = pathinfo($fileName);
  179. $hashStr = substr(md5($pathInfo['basename']),8,5);
  180. $hashName = date('YmdHis').$hashStr.'.'.$pathInfo['extension'];
  181. $uploadPath = $uploadDir.$hashName;
  182. $uploadStr .= $hashName;
  183. if (!$out = @fopen($uploadPath, "wb")) {
  184. getjson('Failed to open output stream.');
  185. }
  186. if ( flock($out, LOCK_EX) ) {
  187. for( $index = 0; $index < $chunks; $index++ ) {
  188. if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
  189. break;
  190. }
  191. while ($buff = fread($in, 4096)) {
  192. fwrite($out, $buff);
  193. }
  194. @fclose($in);
  195. @unlink("{$filePath}_{$index}.part");
  196. }
  197. flock($out, LOCK_UN);
  198. }
  199. @fclose($out);
  200. //video
  201. $data['cid'] = $cid;
  202. $data['name'] = $video_name;
  203. $data['path'] = $uploadPath;
  204. $data['md5'] = md5_file($uploadPath);
  205. $data['addtime'] = time();
  206. $this->load->library('xyz');
  207. $format = $this->xyz->format($uploadPath);
  208. // if(empty($format['size']) || empty($format['duration'])){
  209. // unlink($uploadPath);
  210. // getjson('ffmpeg error');
  211. // }
  212. $data['duration'] = $format['duration'];
  213. $data['size'] = $format['size'];
  214. $this->db->insert("video",$data);
  215. $insert_id = $this->db->insert_id();
  216. //路径
  217. $time = time();
  218. $m3u8path = m3u8_dir($insert_id,$time);
  219. $jpgpath = m3u8_dir($insert_id,$time,'jpg');
  220. $gifpath = m3u8_dir($insert_id,$time,'gif');
  221. $this->db->update('video',array('m3u8'=>$m3u8path,'pic'=>$jpgpath),array('id'=>$insert_id));
  222. //M3U8保存目录
  223. $m3u8_dir = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  224. //queue
  225. $queue2['xu'] = 1;
  226. $queue['vid'] = $insert_id;
  227. $queue['path'] = $uploadPath;
  228. $queue['jpg'] = $m3u8_dir.$jpgpath;
  229. $queue['gif'] = $m3u8_dir.$gifpath;
  230. $queue['m3u8'] = $m3u8_dir.$m3u8path;
  231. $queue['duration'] = $format['duration'];
  232. $this->db->insert('queue',$queue);
  233. //多码率
  234. if(Mu_Type == 1){
  235. $ext = end(explode('/', $m3u8path));
  236. $queue['xu'] = 2;
  237. $queue['m3u8'] = $m3u8_dir.str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8path);
  238. $this->db->insert('queue',$queue);
  239. }
  240. getjson('ok');
  241. }
  242. }
  243. }