国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

php多文件上傳 多圖片上傳程序代碼

2024-09-04 11:45:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

多文件上傳其實(shí)就包括了圖片及各種文件了,下面介紹的是一款PHP多文件上傳類(lèi),一共兩個(gè)文件,upp.php 和 uploadFile.php,upp.php,這是前臺(tái)所顯示的表單文件了,默認(rèn)的是四個(gè)上傳文件域,我們可以手動(dòng)進(jìn)行修改,另外這個(gè)頁(yè)面嵌套了 uploadFile.php 文件上傳類(lèi),下面一起來(lái)看例子.

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,'個(gè)文件上傳成功<br/>'
  14. echo $up->showErrorInfo(); 
  15. exit
  16. ?> 
  17.  
  18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  19. <html xmlns="http://www.111cn.net/ 1999/xhtml"
  20. <head> 
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  22. <title>無(wú)標(biāo)題文檔</title> 
  23. </head> 
  24. <body> 
  25. <form action="uup.php" method="post" enctype="multipart/form-data"
  26. <p><input name="file[]" type="file" /></p> 
  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. <input name="submit" type="submit" /> 
  31. </form> 
  32. </body> 
  33. </html> 

PHP文件上傳類(lèi)代碼,代碼如下:

  1. <?php 
  2. /*------------*/ 
  3. class UploadFile 
  4. var $user_post_file  = array(); 
  5. var $save_file_path  = ''
  6. var $max_file_size   = ''
  7. var $allow_type      = array('gif','jpg','png','zip','rar','txt','doc','pdf'); 
  8. var $final_file_path = ''
  9. var $save_info       = array(); 
  10. var $error_info      = array(); 
  11. /** 
  12. *構(gòu)造函數(shù),用于初始化信息。 
  13. * 
  14. *@param Array $file 
  15. *@param String $path 
  16. *@param Integer $size 
  17. *@param Array $type 
  18. */ 
  19. function __construct($file,$path,$size = 2097152,$type=''
  20. $this->user_post_file   = $file
  21. $this->save_file_path   = $path
  22. $this->max_file_size    = $size
  23. if(!$type=''){ 
  24. $this->allow_type[] = $type
  25. /** 
  26. * 
  27. * 
  28. *@access public 
  29. *@return int 
  30. */ 
  31. function upload() 
  32. for($i=0;$i<count($this->user_post_file['name']);$i++) 
  33. if($this->user_post_file['error'][$i] == 0){//上傳文件狀態(tài)正常 
  34. //獲取當(dāng)前文件名,臨時(shí)文件名,大小,類(lèi)型,擴(kuò)展名 
  35. $name     = $this->user_post_file['name'][$i]; 
  36. $tmp_name = $this->user_post_file['tmp_name'][$i]; 
  37. $size     = $this->user_post_file['size'][$i]; 
  38. $type     = $this->user_post_file['type'][$i]; 
  39. $ext_name = $this->getExtName($name); 
  40. //文件大小 
  41. if(!$this->checkSize($size)){ 
  42. $this->error_info[] = '您上傳的文件:'.$name.'太大'
  43. continue
  44. //擴(kuò)展名 
  45. if(!$this->checkType($ext_name)){ 
  46. $this->error_info[] = '您上傳的文件:'.$name.'不合法'
  47. continue
  48. //非法上傳 
  49. if(!is_uploaded_file($tmp_name)){ 
  50. $this->error_info[] = '您上傳的文件:'.$name.'屬于非法提交'
  51. continue
  52. // 
  53. $basename = $this->getBaseName($name,".".$ext_name); 
  54. $final_filename = $basename.'-'.time().'-'.rand(1,10000).'.'.$ext_name
  55. $this->final_file_path = $this->save_file_path.'/'.$final_filename
  56. if(!move_uploaded_file($tmp_name,$this->final_file_path)){ 
  57. $this->error_info = $this->user_post_file['error'][$i]; 
  58. continue
  59. // 
  60. $this->save_info[] = array
  61. "name" => $name
  62. "ext_name" => $ext_name
  63.           "type" => $type
  64.                             "size" => $size,  
  65. "final_filename" => $final_filename
  66.                             "path" => $this->final_file_path 
  67. );  
  68. return count($this->save_info); 
  69. /* 
  70.  *檢查用戶(hù)上傳文件的大小時(shí)候合法 
  71.  * 
  72.  *@param Integer $size 
  73.  *@access private 
  74.  *@return boolean 
  75.  */ 
  76. function checkSize($size
  77. if($size > $this->max_file_size){ 
  78. return FALSE; 
  79. return TRUE; 
  80. /* 
  81.  *檢查用戶(hù)上傳文件的類(lèi)型是否合法 
  82.  * 
  83.  *@access private 
  84.  *@return boolean 
  85.  */ 
  86. function checkType($extension
  87. foreach($this->allow_type as $type){ 
  88. if(strcasecmp($extension,$type) == 0){ 
  89. return TRUE; 
  90. return FALSE; 
  91. /* 
  92.  *獲取文件的擴(kuò)展名 
  93.  * 
  94.  *@param string $filename 
  95.  *@access private 
  96.  *@return string 
  97.  */ 
  98. function getExtName($filename
  99. $p = pathinfo($filename); 
  100. return $p['extension']; 
  101. /* 
  102.  *獲取文件名(不包括擴(kuò)展名) 
  103.  * 
  104.  *@param string $filename 
  105.  *@param string $type 
  106.  *@access private 
  107.  *@return boolean 
  108.  */ 
  109. function getBaseName($filename,$ext_name
  110. $basename = basename($filename,$ext_name); 
  111. return $basename
  112. /* 
  113.  * 
  114.  * 
  115.  * 
  116.  */ 
  117. function showErrorInfo() 
  118. if(count($this->error_info) != 0){ 
  119. //echo 'error...<br/>'; 
  120. foreach($this->error_info as $k=>$v){ 
  121. echo ($k+1),':',$v,'<br/>'
  122. }//開(kāi)源代碼Vevb.com 
  123. function getSaveInfo() 
  124. return $this->save_info; 
  125. //$upload = new UploadFile('',''); 
  126. //$upload = new UploadFile(); 
  127. //$upload->showErrorInfo(); 
  128. ?>

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚州市| 那坡县| 巩义市| 丽水市| 美姑县| 紫金县| 黄龙县| 万宁市| 东阿县| 商洛市| 京山县| 沁水县| 青神县| 古蔺县| 东宁县| 蓝山县| 宁强县| 厦门市| 定兴县| 曲松县| 永福县| 恩平市| 临城县| 盐边县| 宿州市| 丰原市| 三亚市| 沙湾县| 高阳县| 乌拉特后旗| 芮城县| 合肥市| 七台河市| 固原市| 台江县| 梁平县| 鄂尔多斯市| 辽中县| 永嘉县| 什邡市| 墨竹工卡县|