php多文件上传 多图片上传程序代码

多文件上传其实就包括了图片及各种文件了,下面介绍的是一款PHP多文件上传类,一共两个文件,upp.php 和 uploadFile.php,upp.php,这是前台所显示的表单文件了,默认的是四个上传文件域,我们可以手动进行修改,另外这个页面嵌套了 uploadFile.php 文件上传类,下面一起来看例子.

php文件上传例子,代码如下:

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require('uploadFile.php');
  4. if(isset($_POST['submit'])){
  5. $uploads = $_FILES['file'];
  6. $num_file = count($uploads['name']);
  7. $up = new UploadFile($uploads,'uploads',1024);
  8. $num = $up->upload();
  9. if($num == $num_file ){
  10. echo '全部文件上传成功';
  11. exit;
  12. }else{
  13. echo $num,'个文件上传成功<br/>';
  14. echo $up->showErrorInfo();
  15. exit;
  16. }
  17. }
  18. ?>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  20. <html xmlns="http://www.111cn.net/ 1999/xhtml">
  21. <head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  23. <title>无标题文档</title>
  24. </head>
  25. <body>
  26. <form action="uup.php" method="post" enctype="multipart/form-data">
  27. <p><input name="file[]" type="file" /></p>
  28. <p><input name="file[]" type="file" /></p>
  29. <p><input name="file[]" type="file" /></p>
  30. <p><input name="file[]" type="file" /></p>
  31. <input name="submit" type="submit" />
  32. </form>
  33. </body>
  34. </html>

PHP文件上传类代码,代码如下:

  1. <?php
  2. /*------------*/
  3. class UploadFile
  4. {
  5. var $user_post_file = array();
  6. var $save_file_path = '';
  7. var $max_file_size = '';
  8. var $allow_type = array('gif','jpg','png','zip','rar','txt','doc','pdf');
  9. var $final_file_path = '';
  10. var $save_info = array();
  11. var $error_info = array();
  12. /**
  13. *构造函数,用于初始化信息。
  14. *
  15. *@param Array $file
  16. *@param String $path
  17. *@param Integer $size
  18. *@param Array $type
  19. */
  20. function __construct($file,$path,$size = 2097152,$type='')
  21. {
  22. $this->user_post_file = $file;
  23. $this->save_file_path = $path;
  24. $this->max_file_size = $size;
  25. if(!$type=''){
  26. $this->allow_type[] = $type;
  27. }
  28. }
  29. /**
  30. *
  31. *
  32. *@access public
  33. *@return int
  34. */
  35. function upload()
  36. {
  37. for($i=0;$i<count($this->user_post_file['name']);$i++)
  38. {
  39. if($this->user_post_file['error'][$i] == 0){//上传文件状态正常
  40. //获取当前文件名,临时文件名,大小,类型,扩展名
  41. $name = $this->user_post_file['name'][$i];
  42. $tmp_name = $this->user_post_file['tmp_name'][$i];
  43. $size = $this->user_post_file['size'][$i];
  44. $type = $this->user_post_file['type'][$i];
  45. $ext_name = $this->getExtName($name);
  46. //文件大小
  47. if(!$this->checkSize($size)){
  48. $this->error_info[] = '您上传的文件:'.$name.'太大';
  49. continue;
  50. }
  51. //扩展名
  52. if(!$this->checkType($ext_name)){
  53. $this->error_info[] = '您上传的文件:'.$name.'不合法';
  54. continue;
  55. }
  56. //非法上传
  57. if(!is_uploaded_file($tmp_name)){
  58. $this->error_info[] = '您上传的文件:'.$name.'属于非法提交';
  59. continue;
  60. }
  61. //
  62. $basename = $this->getBaseName($name,".".$ext_name);
  63. $final_filename = $basename.'-'.time().'-'.rand(1,10000).'.'.$ext_name;
  64. $this->final_file_path = $this->save_file_path.'/'.$final_filename;
  65. if(!move_uploaded_file($tmp_name,$this->final_file_path)){
  66. $this->error_info = $this->user_post_file['error'][$i];
  67. continue;
  68. }
  69. //
  70. $this->save_info[] = array(
  71. "name" => $name,
  72. "ext_name" => $ext_name,
  73. "type" => $type,
  74. "size" => $size,
  75. "final_filename" => $final_filename,
  76. "path" => $this->final_file_path
  77. );
  78. }
  79. }
  80. return count($this->save_info);
  81. }
  82. /*
  83. *检查用户上传文件的大小时候合法
  84. *
  85. *@param Integer $size
  86. *@access private
  87. *@return boolean
  88. */
  89. function checkSize($size)
  90. {
  91. if($size > $this->max_file_size){
  92. return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. /*
  97. *检查用户上传文件的类型是否合法
  98. *
  99. *@access private
  100. *@return boolean
  101. */
  102. function checkType($extension)
  103. {
  104. foreach($this->allow_type as $type){
  105. if(strcasecmp($extension,$type) == 0){
  106. return TRUE;
  107. }
  108. }
  109. return FALSE;
  110. }
  111. /*
  112. *获取文件的扩展名
  113. *
  114. *@param string $filename
  115. *@access private
  116. *@return string
  117. */
  118. function getExtName($filename)
  119. {
  120. $p = pathinfo($filename);
  121. return $p['extension'];
  122. }
  123. /*
  124. *获取文件名(不包括扩展名)
  125. *
  126. *@param string $filename
  127. *@param string $type
  128. *@access private
  129. *@return boolean
  130. */
  131. function getBaseName($filename,$ext_name)
  132. {
  133. $basename = basename($filename,$ext_name);
  134. return $basename;
  135. }
  136. /*
  137. *
  138. *
  139. *
  140. */
  141. function showErrorInfo()
  142. {
  143. if(count($this->error_info) != 0){
  144. //echo 'error...<br/>';
  145. foreach($this->error_info as $k=>$v){
  146. echo ($k+1),':',$v,'<br/>';
  147. }//开源代码phpfensi.com
  148. }
  149. }
  150. function getSaveInfo()
  151. {
  152. return $this->save_info;
  153. }
  154. }
  155. //$upload = new UploadFile('','');
  156. //$upload = new UploadFile();
  157. //$upload->showErrorInfo();
  158. ?>