upfile_ex.php 1.4 KB

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