PHP实现的多文件上传类及用法示例,
本文实例讲述了PHP实现的多文件上传类及用法。分享给大家供大家参考,具体如下:
1、upFiles.css.php 文件
<?php class UploadFiles{ private $maxsize = '1000000'; //允许上传文件最大长度 private $allowtype = array('jpg','png','gif','jpeg');//允许上传文件类型 private $israndfile = true;//是否随机文件名 private $filepath;//上传路径 private $originName;//上传的源文件 private $tmpfileName;//临时文件名 private $newfileName;//新文件名 private $fileSize;//文件大小 private $fileType;//文件类型 private $errorNum = 0;//错误号 private $errorMessg = array();//错误消息 //对成员初始化 function __construct($options = array()){ foreach($options as $key=>$val){ $key = strtolower($key); //查看传进来的数组里下标是否与成员属性相同 //print_r(array_keys(get_class_vars(get_class($this)))); if(!in_array($key,array_keys(get_class_vars(get_class($this))))){ continue; }else{ $this->setOption($key,$val); } } } private function setOption($key,$val){ $this->$key = $val; //echo $this->errorNum."<br>"; } //检查文件上传路径 private function checkfilePath(){ //echo $this->filepath; if(empty($this->filepath)){ $this->setOption('errorNum',"-5"); return false; } if(!file_exists($this->filepath) || !is_writable($this->filepath)){ if(!@mkdir($this->filepath,0755)){ $this->setOption('errorNum','-4'); return false; } } return true; } //获取错误信息 private function getError(){ $str = "上传文件{$this->originName}出错---"; switch($this->errorNum){ case 4; $str .= "没有文件被上传";break; case 3; $str .= "文件只被部分上传";break; case 2; $str .= "超过文件表单允许大小";break; case 1; $str .= "超过php.ini中允许大小";break; case -1; $str .= "未允许的类型";break; case -2; $str .= "文件过大,不能超过".$this->maxsize."个字节";break; case -3; $str .= "上传失败";break; case -4; $str .= "建立文件上传目录失败";break; case -5; $str .= "必须指定上传路径";break; default; $str .= "未知错误"; } return $str."<br>"; } //检查文件类型 private function checkfileType(){ //echo $this->fileType; if(!in_array(strtolower($this->fileType),$this->allowtype)){ $this->setOption('errorNum','-1'); return false; }else{ return true; } } //检查文件大小 private function checkfileSize(){ if($this->fileSize > $this->maxsize){ $this->setOption('errorNum','-2'); return false; }else{ return true; } } //处理随机文件名称 private function prorandFile(){ $ch = $this->israndfile; if($ch == 'true'){ return true; }else{ return false; } } // private function setFiles($name="",$tmp_name="",$size="",$error=""){ //检查上传路径 if(!$this->checkfilePath()){ //$this->errorMessg = $this->getError(); return false; } //echo $error."<br>"; if($error){ $this->setOption('errorNum',$error); return false; } $arrstr = explode('.',$name); $type = end($arrstr); $this->setOption('originName',$name); $this->setOption('fileSize',$size); $this->setOption('fileType',$type); $this->setOption('tmpfileName',$tmp_name); return true; } //检查是否有文件上传 function checkFile($formname){ if(!@$_FILES[$formname]){ $this->setOption('errorNum',4); return false; }else{ return true; } } //上传文件 function uploadeFile($formname){ if(!$this->checkFile($formname)){ $this->errorMessg = $this->getError(); return false; } $return = true; $name = @$_FILES[$formname]['name']; $tmp_name = @$_FILES[$formname]['tmp_name']; $size = @$_FILES[$formname]['size']; $error = @$_FILES[$formname]['error']; //$type = $_FILES[$formname]['type']; if(is_array($name)){ $errors = array(); for($i=0; $i<count($name); $i++){ if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){ if(!$this->checkfileSize() || !$this->checkfileType()){ $errors[] = $this->getError(); $return = false; } }else{ $errors[] = $this->getError(); $return = false; } if(!$return) $this->setFiles(); } if($return){ $newfileN = array(); for($i=0; $i<count($name); $i++){ if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){ if(!$this->copyFile()){ $errors[] = $this->getError(); $return = false; }else{ $newfileN[] = $this->newfileName; } } $this->newfileName = $newfileN; } } //print_r($errors); $this->errorMessg = $errors; //echo $errors; return $return; }else{ if($this->setFiles($name,$tmp_name,$size,$error)){ $return = true; if($error) var_dump($error); if($this->checkfileSize() && $this->checkfileType()){ }else{ $return = false; } }else{ $return = false; } if(!$return){ $this->errorMessg = $this->getError(); } return $return; } } //获取上传后的文件名 function getnewFile(){ return $this->newfileName; } //把文件拷贝到指定的路径 function copyFile(){ $filepath = rtrim($this->filepath,'/')."/"; if(!$this->errorNum){ if($this->prorandFile()){ $this->newfileName = date('Ymdhis').rand(1000,9999).".".$this->fileType; }else{ $this->newfileName = $this->originName; } if(!move_uploaded_file($this->tmpfileName,$filepath.$this->newfileName)){ $this->setOption('errorNum',-3); return false; }else{ return true; } }else{ return false; } } //上传错误后返回的消息 function gteerror(){ $err = $this->errorMessg; return $err; } } ?>
2、使用方法
uploade.php 文件:
<?php //print_r($_FILES['spic']); header('Content-Type:text/html;charset=utf-8'); //if(@$_FILES['spic'])echo "ddddddddd";; include('upFiles.css.php'); $upfile = new UploadFiles(array('filepath'=>'./upload','allowtype'=>array('php','bmp','gif','jpg','png'),'israndfile'=>true,'maxsize'=>'1000000')); if($upfile ->uploadeFile('spic')){ $arrfile = $upfile ->getnewFile(); foreach($arrfile as $v){ echo $v,"<br/>"; } echo "上传成功!"; }else{ $err = $upfile ->gteerror(); if(is_array($err)){ foreach($err as $v1){ echo $v1,"<br/>"; } }else{ echo $err; } //var_dump($err); } //var_dump($upfile); ?>
HTML 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> function Check(){ //alert('dddd'); for(i=1; i<9; i++){ if(document.getElementById('v'+i).value == ''){ document.getElementById('v'+i).name = 'uu'; } } } </script> </head> <body> <form name="upfile" action="uploade.php" method="post" enctype="multipart/form-data"> <input type="file" name="spic[]" id="v1" /><br/> <input type="file" name="spic[]" id="v2" /><br/> <input type="file" name="spic[]" id="v3" /><br/> <input type="file" name="spic[]" id="v4" /><br/> <input type="file" name="spic[]" id="v5" /><br/> <input type="file" name="spic[]" id="v6" /><br/> <input type="file" name="spic[]" id="v7" /><br/> <input type="file" name="spic[]" id="v8" /><br/> <input type="submit" name="sub" value="提交" onclick="return Check()" /> <input type="reset" name="res" value="重填" /> </form> </body> </html>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。