当前位置:Gxlcms > PHP教程 > php多文件上传类及用法实例详解

php多文件上传类及用法实例详解

时间:2021-07-01 10:21:17 帮助过:23人阅读

1、upFiles.css.php 文件

  1. <?php
  2. class UploadFiles{
  3. private $maxsize = '1000000'; //允许上传文件最大长度
  4. private $allowtype = array('jpg','png','gif','jpeg');//允许上传文件类型
  5. private $israndfile = true;//是否随机文件名
  6. private $filepath;//上传路径
  7. private $originName;//上传的源文件
  8. private $tmpfileName;//临时文件名
  9. private $newfileName;//新文件名
  10. private $fileSize;//文件大小
  11. private $fileType;//文件类型
  12. private $errorNum = 0;//错误号
  13. private $errorMessg = array();//错误消息
  14. //对成员初始化
  15. function construct($options = array()){
  16. foreach($options as $key=>$val){
  17. $key = strtolower($key);
  18. //查看传进来的数组里下标是否与成员属性相同
  19. //print_r(array_keys(get_class_vars(get_class($this))));
  20. if(!in_array($key,array_keys(get_class_vars(get_class($this))))){
  21. continue;
  22. }else{
  23. $this->setOption($key,$val);
  24. }
  25. }
  26. }
  27. private function setOption($key,$val){
  28. $this->$key = $val;
  29. //echo $this->errorNum."<br>";
  30. }
  31. //检查文件上传路径
  32. private function checkfilePath(){
  33. //echo $this->filepath;
  34. if(empty($this->filepath)){
  35. $this->setOption('errorNum',"-5");
  36. return false;
  37. }
  38. if(!file_exists($this->filepath) || !is_writable($this->filepath)){
  39. if(!@mkdir($this->filepath,0755)){
  40. $this->setOption('errorNum','-4');
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. //获取错误信息
  47. private function getError(){
  48. $str = "上传文件{$this->originName}出错---";
  49. switch($this->errorNum){
  50. case 4; $str .= "没有文件被上传";break;
  51. case 3; $str .= "文件只被部分上传";break;
  52. case 2; $str .= "超过文件表单允许大小";break;
  53. case 1; $str .= "超过php.ini中允许大小";break;
  54. case -1; $str .= "未允许的类型";break;
  55. case -2; $str .= "文件过大,不能超过".$this->maxsize."个字节";break;
  56. case -3; $str .= "上传失败";break;
  57. case -4; $str .= "建立文件上传目录失败";break;
  58. case -5; $str .= "必须指定上传路径";break;
  59. default; $str .= "未知错误";
  60. }
  61. return $str."<br>";
  62. }
  63. //检查文件类型
  64. private function checkfileType(){
  65. //echo $this->fileType;
  66. if(!in_array(strtolower($this->fileType),$this->allowtype)){
  67. $this->setOption('errorNum','-1');
  68. return false;
  69. }else{
  70. return true;
  71. }
  72. }
  73. //检查文件大小
  74. private function checkfileSize(){
  75. if($this->fileSize > $this->maxsize){
  76. $this->setOption('errorNum','-2');
  77. return false;
  78. }else{
  79. return true;
  80. }
  81. }
  82. //处理随机文件名称
  83. private function prorandFile(){
  84. $ch = $this->israndfile;
  85. if($ch == 'true'){
  86. return true;
  87. }else{
  88. return false;
  89. }
  90. }
  91. //
  92. private function setFiles($name="",$tmp_name="",$size="",$error=""){
  93. //检查上传路径
  94. if(!$this->checkfilePath()){
  95. //$this->errorMessg = $this->getError();
  96. return false;
  97. }
  98. //echo $error."<br>";
  99. if($error){
  100. $this->setOption('errorNum',$error);
  101. return false;
  102. }
  103. $arrstr = explode('.',$name);
  104. $type = end($arrstr);
  105. $this->setOption('originName',$name);
  106. $this->setOption('fileSize',$size);
  107. $this->setOption('fileType',$type);
  108. $this->setOption('tmpfileName',$tmp_name);
  109. return true;
  110. }
  111. //检查是否有文件上传
  112. function checkFile($formname){
  113. if(!@$_FILES[$formname]){
  114. $this->setOption('errorNum',4);
  115. return false;
  116. }else{
  117. return true;
  118. }
  119. }
  120. //上传文件
  121. function uploadeFile($formname){
  122. if(!$this->checkFile($formname)){
  123. $this->errorMessg = $this->getError();
  124. return false;
  125. }
  126. $return = true;
  127. $name = @$_FILES[$formname]['name'];
  128. $tmp_name = @$_FILES[$formname]['tmp_name'];
  129. $size = @$_FILES[$formname]['size'];
  130. $error = @$_FILES[$formname]['error'];
  131. //$type = $_FILES[$formname]['type'];
  132. if(is_array($name)){
  133. $errors = array();
  134. for($i=0; $i<count($name); $i++){
  135. if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
  136. if(!$this->checkfileSize() || !$this->checkfileType()){
  137. $errors[] = $this->getError();
  138. $return = false;
  139. }
  140. }else{
  141. $errors[] = $this->getError();
  142. $return = false;
  143. }
  144. if(!$return) $this->setFiles();
  145. }
  146. if($return){
  147. $newfileN = array();
  148. for($i=0; $i<count($name); $i++){
  149. if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
  150. if(!$this->copyFile()){
  151. $errors[] = $this->getError();
  152. $return = false;
  153. }else{
  154. $newfileN[] = $this->newfileName;
  155. }
  156. }
  157. $this->newfileName = $newfileN;
  158. }
  159. }
  160. //print_r($errors);
  161. $this->errorMessg = $errors;
  162. //echo $errors;
  163. return $return;
  164. }else{
  165. if($this->setFiles($name,$tmp_name,$size,$error)){
  166. $return = true;
  167. if($error) var_dump($error);
  168. if($this->checkfileSize() && $this->checkfileType()){
  169. }else{
  170. $return = false;
  171. }
  172. }else{
  173. $return = false;
  174. }
  175. if(!$return){
  176. $this->errorMessg = $this->getError();
  177. }
  178. return $return;
  179. }
  180. }
  181. //获取上传后的文件名
  182. function getnewFile(){
  183. return $this->newfileName;
  184. }
  185. //把文件拷贝到指定的路径
  186. function copyFile(){
  187. $filepath = rtrim($this->filepath,'/')."/";
  188. if(!$this->errorNum){
  189. if($this->prorandFile()){
  190. $this->newfileName = date('Ymdhis').rand(1000,9999).".".$this->fileType;
  191. }else{
  192. $this->newfileName = $this->originName;
  193. }
  194. if(!move_uploaded_file($this->tmpfileName,$filepath.$this->newfileName)){
  195. $this->setOption('errorNum',-3);
  196. return false;
  197. }else{
  198. return true;
  199. }
  200. }else{
  201. return false;
  202. }
  203. }
  204. //上传错误后返回的消息
  205. function gteerror(){
  206. $err = $this->errorMessg;
  207. return $err;
  208. }
  209. }
  210. ?>

2、使用方法

uploade.php 文件:

  1. <?php
  2. //print_r($_FILES['spic']);
  3. header('Content-Type:text/html;charset=utf-8');
  4. //if(@$_FILES['spic'])echo "ddddddddd";;
  5. include('upFiles.css.php');
  6. $upfile = new UploadFiles(array('filepath'=>'./upload','allowtype'=>array('php','bmp','gif','jpg','png'),'israndfile'=>true,'maxsize'=>'1000000'));
  7. if($upfile ->uploadeFile('spic')){
  8. $arrfile = $upfile ->getnewFile();
  9. foreach($arrfile as $v){
  10. echo $v,"<br/>";
  11. }
  12. echo "上传成功!";
  13. }else{
  14. $err = $upfile ->gteerror();
  15. if(is_array($err)){
  16. foreach($err as $v1){
  17. echo $v1,"<br/>";
  18. }
  19. }else{
  20. echo $err;
  21. }
  22. //var_dump($err);
  23. }
  24. //var_dump($upfile);
  25. ?>

HTML 文件:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>无标题文档</title>
  7. <script type="text/javascript">
  8. function Check(){
  9. //alert('dddd');
  10. for(i=1; i<9; i++){
  11. if(document.getElementById('v'+i).value == ''){
  12. document.getElementById('v'+i).name = 'uu';
  13. }
  14. }
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form name="upfile" action="uploade.php" method="post" enctype="multipart/form-data">
  20. <input type="file" name="spic[]" id="v1" /><br/>
  21. <input type="file" name="spic[]" id="v2" /><br/>
  22. <input type="file" name="spic[]" id="v3" /><br/>
  23. <input type="file" name="spic[]" id="v4" /><br/>
  24. <input type="file" name="spic[]" id="v5" /><br/>
  25. <input type="file" name="spic[]" id="v6" /><br/>
  26. <input type="file" name="spic[]" id="v7" /><br/>
  27. <input type="file" name="spic[]" id="v8" /><br/>
  28. <input type="submit" name="sub" value="提交" onclick="return Check()" />
  29. <input type="reset" name="res" value="重填" />
  30. </form>
  31. </body>
  32. </html>

以上就是php多文件上传类及用法实例详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行