fileUp.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: taoqili
  5. * Date: 12-2-8
  6. * Time: 下午1:20
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. error_reporting(E_ERROR|E_WARNING);
  10. //上传配置
  11. $config = array(
  12. "uploadPath" => "upload/" , //保存路径
  13. "fileType" => array( ".rar" , ".doc" , ".docx" , ".zip" , ".pdf" , ".txt" , ".swf", ".wmv" ) , //文件允许格式
  14. "fileSize" => 100 //文件大小限制,单位MB
  15. );
  16. //文件上传状态,当成功时返回SUCCESS,其余值将直接返回对应字符窜
  17. $state = "SUCCESS";
  18. $fileName = "";
  19. $path = $config[ 'uploadPath' ];
  20. if ( !file_exists( $path ) ) {
  21. mkdir( "$path" , 0777 );
  22. }
  23. $clientFile = $_FILES[ "upfile" ];
  24. if(!isset($clientFile)){
  25. echo "{'state':'文件大小超出服务器配置!','url':'null','fileType':'null'}";//请修改php.ini中的upload_max_filesize和post_max_size
  26. exit;
  27. }
  28. //格式验证
  29. $current_type = strtolower( strrchr( $clientFile[ "name" ] , '.' ) );
  30. if ( !in_array( $current_type , $config[ 'fileType' ] ) ) {
  31. $state = "不支持的文件类型!";
  32. }
  33. //大小验证
  34. $file_size = 1024 * 1024 * $config[ 'fileSize' ];
  35. if ( $clientFile[ "size" ] > $file_size ) {
  36. $state = "文件大小超出限制!";
  37. }
  38. //保存文件
  39. if ( $state == "SUCCESS" ) {
  40. $tmp_file = $clientFile[ "name" ];
  41. $fileName = $path . rand( 1 , 10000 ) . time() . strrchr( $tmp_file , '.' );
  42. $result = move_uploaded_file( $clientFile[ "tmp_name" ] , $fileName );
  43. if ( !$result ) {
  44. $state = "文件保存失败!";
  45. }
  46. }
  47. //向浏览器返回数据json数据
  48. echo '{"state":"' . $state . '","url":"' . $fileName . '","fileType":"' . $current_type . '","original":"'.$clientFile["name"] .'"}';
  49. ?>