PHP实现常用文件上传

在PHP的使用中,它有着非常方便的操作设计,这次文章就给大家介绍下怎么使用PHP实现常用文件上传,相信这也是大多数人会遇到的问题,下面我们具体看看上传方法。

  1. <?php
  2. /**
  3. * 上传文件类
  4. * @param _path : 服务器文件存放路径
  5. * @param _allowType : 允许上传的文件类型和所对应的MIME
  6. * @param _file : 上传的文件信息
  7. */
  8. class Upload{
  9. private $_path;
  10. private $_allowType;
  11. private $_file;
  12. /**
  13. * 构造函数
  14. * @param string : 服务器上存放上传文件的路径
  15. */
  16. function __construct( $path = '' )
  17. {
  18. $this->_path = $path;
  19. $this->_allowType = array(
  20. // images
  21. 'bmp' => 'image/x-ms-bmp',
  22. 'jpg' => 'image/jpeg',
  23. 'jpeg' => 'image/jpeg',
  24. 'gif' => 'image/gif',
  25. 'png' => 'image/png',
  26. 'tif' => 'image/tiff',
  27. 'tiff' => 'image/tiff',
  28. 'tga' => 'image/x-targa',
  29. 'psd' => 'image/vnd.adobe.photoshop',
  30. //文本
  31. 'txt' => 'text/plain',
  32. 'php' => 'text/x-php',
  33. 'html' => 'text/html',
  34. 'htm' => 'text/html',
  35. 'js' => 'text/javascript',
  36. '<a href="http://www.phpfensi.com/cssdiv/css.html" class="anchor" target="_blank">css</a>' => 'text/css',
  37. 'rtf' => 'text/rtf',
  38. 'rtfd' => 'text/rtfd',
  39. 'py' => 'text/x-python',
  40. 'java' => 'text/x-java-source',
  41. 'rb' => 'text/x-ruby',
  42. 'sh' => 'text/x-shellscript',
  43. 'pl' => 'text/x-perl',
  44. 'sql' => 'text/x-sql',
  45. //应用
  46. 'exe' => 'application/octet-stream',
  47. 'doc' => 'application/vnd.ms-word',
  48. 'docx' => 'application/vnd.ms-word',
  49. 'xls' => 'application/vnd.ms-excel',
  50. 'ppt' => 'application/vnd.ms-powerpoint',
  51. 'pps' => 'application/vnd.ms-powerpoint',
  52. 'pdf' => 'application/pdf',
  53. 'xml' => 'application/xml',
  54. //音频
  55. 'mp3' => 'audio/mpeg',
  56. 'mid' => 'audio/midi',
  57. 'ogg' => 'audio/ogg',
  58. 'mp4a' => 'audio/mp4',
  59. 'wav' => 'audio/wav',
  60. 'wma' =&ggt; 'audio/x-ms-wma',
  61. //视频
  62. 'avi' => 'video/x-msvideo',
  63. 'dv' => 'video/x-dv',
  64. 'mp4' => 'video/mp4',
  65. 'mpeg' => 'video/mpeg',
  66. 'mpg' => 'video/mpeg',
  67. 'mov' => 'video/quicktime',
  68. 'wm' => 'video/x-ms-wmv',
  69. 'flv' => 'video/x-flv',
  70. 'mkv' => 'video/x-matroska'
  71. );
  72. }
  73. /**
  74. * 上传函数
  75. * @param string : 表单元素的name 值
  76. * @return [type]
  77. */
  78. public function upload( $txtName = '' )
  79. {
  80. $this->_file = $_FILES[$txtName];
  81. if( $this->_file['error'] == 0){
  82. $fileType = end( explode('.', $this->_file['name'] ));
  83. $allowType = array();
  84. foreach( $this->_allowType as $item=>$value ){
  85. $allowType[] = $item;
  86. }
  87. if( !in_array($fileType, $allowType)){
  88. die('上传的文件格式不正确!');
  89. }else{
  90. if(move_uploaded_file($this->file['tmp_name'], ($this->path).$this->file['name']))
  91. {
  92. echo "<script>alert('上传成功!')</script>";
  93. }
  94. else
  95. {
  96. echo "<script>alert('上传失败!');</script>";
  97. }
  98. }
  99. }else{
  100. //没有正确上传
  101. switch ($this->file['error']){
  102. case 1:
  103. die('文件大小超过系统限制。');
  104. break;
  105. case 2:
  106. die('文件大小超过预定义限制。');
  107. break;
  108. case 3:
  109. die('文件为完全上传。');
  110. break;
  111. case 4:
  112. die('未上传任何文件。');
  113. break;
  114. default:
  115. die('上传出错');
  116. break;
  117. }
  118. }
  119. }
  120. //end upload
  121. }