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

首頁 > 開發 > PHP > 正文

適用于初學者的簡易PHP文件上傳類

2024-05-04 23:39:58
字體:
來源:轉載
供稿:網友

這篇文章主要為大家分享了一個適用于初學者的簡易PHP文件上傳類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例講述了PHP多文件上傳類,分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. class Test_Upload{ 
  3.  
  4. protected $_uploaded = array(); 
  5. protected $_destination;  
  6. protected $_max = 1024000; 
  7. protected $_messages = array(); 
  8. protected $_permited = array( 
  9. 'image/gif'
  10. 'image/jpeg'
  11. 'image/pjpeg'
  12. 'image/png' 
  13. ); 
  14. protected $_renamed = false
  15.  
  16. /** 
  17.  
  18. * @param mix $path 
  19.  
  20. */ 
  21. public function __construct($path){ 
  22.  
  23. if (!is_dir($path) || !is_writable($path)){ 
  24. throw new Exception("文件名不可寫,或者不是目錄!"); 
  25. $this->_destination = $path; 
  26. $this->_uploaded = $_FILES; 
  27. /** 
  28. * 移動文件 
  29.  
  30. */ 
  31. public function move(){ 
  32.  
  33. $filed = current($this->_uploaded);  
  34.  
  35. $isOk = $this->checkError($filed['name'], $filed['error']); 
  36. //debug ok 
  37. if ($isOk){ 
  38. $sizeOk = $this->checkSize($filed['name'], $filed['size']); 
  39. $typeOk = $this->checkType($filed['name'], $filed['type']); 
  40. if ($sizeOk && $typeOk){ 
  41.  
  42. $success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']); 
  43.  
  44. if ($success){ 
  45. $this->_messages[] = $filed['name']."文件上傳成功"
  46. }else { 
  47. $this->_messages[] = $filed['name']."文件上傳失敗"
  48.  
  49. /** 
  50. * 查詢messages數組內容  
  51. * 
  52. */ 
  53. public function getMessages(){ 
  54. return $this->_messages; 
  55.  
  56. /** 
  57. * 檢測上傳的文件大小 
  58. * @param mix $string 
  59. * @param int $size 
  60. */ 
  61. public function checkSize($filename, $size){ 
  62.  
  63. if ($size == 0){ 
  64. return false
  65. }else if ($size > $this->_max){ 
  66. $this->_messages[] = "文件超出上傳限制大小".$this->getMaxsize(); 
  67. return false
  68. }else {  
  69. return true
  70.  
  71. /** 
  72. * 檢測上傳文件的類型 
  73. * @param mix $filename 
  74. * @param mix $type 
  75. */ 
  76. protected function checkType($filename, $type){ 
  77. if (!in_array($type, $this->_permited)){ 
  78. $this->_messages[] = "該文件類型是不被允許的上傳類型"
  79. return false
  80. }else { 
  81. return true
  82.  
  83. /** 
  84. * 獲取文件大小 
  85.  
  86. */ 
  87. public function getMaxsize(){ 
  88. return number_format($this->_max / 1024, 1).'KB'
  89.  
  90. /** 
  91. * 檢測上傳錯誤 
  92. * @param mix $filename 
  93. * @param int $error 
  94.  
  95. */ 
  96. public function checkError($filename, $error){ 
  97. switch ($error){ 
  98. case 0 : return true
  99. case 1 : 
  100. case 2 : $this->_messages[] = "文件過大!"return true
  101. case 3 : $this->_messages[] = "錯誤上傳文件!";return false
  102. case 4 : $this->_messages[] = "沒有選擇文件!"return false
  103. default : $this->_messages[] = "系統錯誤!"return false
  104. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凯里市| 卓资县| 灵丘县| 尼勒克县| 夏河县| 岫岩| 海门市| 阳高县| 台南县| 西充县| 汉源县| 依安县| 洪江市| 泰兴市| 洛隆县| 东源县| 谷城县| 苏尼特左旗| 上饶市| 玉溪市| 虞城县| 金秀| 阿克| 秦皇岛市| 江永县| 固始县| 延庆县| 普兰店市| 阳西县| 开化县| 洪湖市| 津南区| 深水埗区| 平原县| 时尚| 临澧县| 泸水县| 南皮县| 黑龙江省| 乐陵市| 自治县|