upfile_ex.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
  3. function upfile_joutput_data($datas)
  4. {
  5. $data = array();
  6. $data['code'] = 200;
  7. $data['message'] = 'SUCCESS';
  8. $data['datas'] = $datas;
  9. ob_clean();
  10. echo(json_encode($data));
  11. }
  12. function upfile_joutput_error()
  13. {
  14. $data = array();
  15. $data['code'] = 15000;
  16. $data['message'] = "上传文件失败.";
  17. $data['datas'] = null;
  18. ob_clean();
  19. echo(json_encode($data));
  20. }
  21. function create_uuid($prefix = "") {
  22. $str = md5(uniqid(mt_rand(), true));
  23. $uuid = substr($str,0,8) . '-';
  24. $uuid .= substr($str,8,4) . '-';
  25. $uuid .= substr($str,12,4) . '-';
  26. $uuid .= substr($str,16,4) . '-';
  27. $uuid .= substr($str,20,12);
  28. return $prefix . $uuid;
  29. }
  30. if ($_POST)
  31. {
  32. $base_path = BASE_ROOT_PATH . '/data/upload/uploadtmp';
  33. $ret = array();
  34. if ($_FILES["file"]["error"] > 0) {
  35. upfile_joutput_error();
  36. }
  37. else
  38. {
  39. $fn = $_FILES["file"]["name"];
  40. $ext_name = '.' . strtolower(pathinfo($fn)['extension']);
  41. $dest_file = $base_path . '/' . date("YmdHis") . '-' . create_uuid() . $ext_name;
  42. $result = move_uploaded_file($_FILES["file"]["tmp_name"],$dest_file);
  43. if ($result) {
  44. $ret['file_path'] = str_replace($base_path, '', $dest_file);
  45. $ret['src_name'] = basename($dest_file);
  46. upfile_joutput_data($ret);
  47. } else {
  48. upfile_joutput_error();
  49. }
  50. }
  51. }