PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/public/ueditor/server/upload/php/up.php

https://github.com/Rming/dc
PHP | 41 lines | 31 code | 4 blank | 6 comment | 6 complexity | 076709582a10a8909e77b91156166e83 MD5 | raw file
  1. <?php
  2. //上传配置
  3. $config = array(
  4. "uploadPath"=>"../../../../../uploads/images/", //保存路径
  5. "fileType"=>array(".gif",".png",".jpg",".jpeg",".bmp"), //文件允许格式
  6. "fileSize"=>1000 //文件大小限制,单位KB
  7. );
  8. //文件上传状态,初始默认成功,可选参数{"SUCCESS","ERROR","SIZE","TYPE"}
  9. $state = "SUCCESS";
  10. $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
  11. $path = $config['uploadPath'];
  12. if(!file_exists($path)){
  13. mkdir("$path", 0777);
  14. }
  15. //格式验证
  16. $current_type = strtolower(strrchr($_FILES["picdata"]["name"], '.'));
  17. if(!in_array($current_type, $config['fileType'])){
  18. $state = "TYPE";
  19. }
  20. //大小验证
  21. $file_size = 1024 * $config['fileSize'];
  22. if( $_FILES["picdata"]["size"] > $file_size ){
  23. $state = "SIZE";
  24. }
  25. //保存图片
  26. if($state == "SUCCESS"){
  27. $tmp_file=$_FILES["picdata"]["name"];
  28. $file = $path.rand(1,10000).time().strrchr($tmp_file,'.');
  29. $result = move_uploaded_file($_FILES["picdata"]["tmp_name"],$file);
  30. if(!$result){
  31. $state = "ERROR";
  32. }
  33. }
  34. //向浏览器返回数据json数据
  35. $file= str_replace('../','',$file); //为方便理解,替换掉所有类似../和./等相对路径标识
  36. echo "{'url':'".$file."','title':'".$title."','state':'".$state."'}";
  37. ?>