php多文件上傳封裝_PHP教程
推薦:php生成圓角圖片的方法具體如下: 代碼如下:?php $image_file = $_GET['src']; $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == no) ? false : true; // Top-l
多文件的上傳實(shí)現(xiàn)
1 利用單文件封裝
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="doAction5.php" method="post" enctype="multipart/form-data">
- 請選擇您要上傳的文件:<input type="file" name="myFile1" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile2" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile3" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile4" /><br/>
- <input type="submit" value="上傳"/>
- </form>
- </body>
- </html>
這里的思路,從print_r($_FILES)中去找,打印出來看到是個(gè)二維數(shù)組,很簡單,遍歷去用就好了!
- <?php
- //print_r($_FILES);
- header('content-type:text/html;charset=utf-8');
- include_once 'upFunc.php';
- foreach ($_FILES as $fileInfo){
- $file[]=uploadFile($fileInfo);
- }
上面那個(gè)function的定義改一下,給定一些默認(rèn)值
- function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){
這樣子,簡單是簡單,但遇到一些問題。
正常的上傳4個(gè)圖片是沒問題,但要是中間激活了函數(shù)中的exit,就會(huì)立即停止,導(dǎo)致其他圖片也無法上傳。
2 升級版封裝
旨在實(shí)現(xiàn)針對多個(gè)或單個(gè)文件上傳的封裝
首先這樣子寫個(gè)靜態(tài)文件
打印查看一下$_FILES數(shù)組內(nèi)容
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="doAction5.php" method="post" enctype="multipart/form-data">
- 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/>
- 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/>
- <input type="submit" value="上傳"/>
- </form>
- </body>
- </html>
- Array
- (
- [myFile] => Array
- (
- [name] => Array
- (
- [0] => test32.png
- [1] => test32.png
- [2] => 333.png
- [3] => test41.png
- )
- [type] => Array
- (
- [0] => image/png
- [1] => image/png
- [2] => image/png
- [3] => image/png
- )
- [tmp_name] => Array
- (
- [0] => D:\wamp\tmp\php831C.tmp
- [1] => D:\wamp\tmp\php834C.tmp
- [2] => D:\wamp\tmp\php837C.tmp
- [3] => D:\wamp\tmp\php83BB.tmp
- )
- [error] => Array
- (
- [0] => 0
- [1] => 0
- [2] => 0
- [3] => 0
- )
- [size] => Array
- (
- [0] => 46174
- [1] => 46174
- [2] => 34196
- [3] => 38514
- )
- )
- )
可以得到一個(gè)三維數(shù)組。
復(fù)雜是復(fù)雜了,但復(fù)雜的有規(guī)律,各項(xiàng)數(shù)值都在一起了,很方便我們?nèi)≈担。?/p>
所以先得到文件信息,變成單文件處理那種信息
然后之前的那種exit錯(cuò)誤,就把exit改一下就好了,這里用res
- function getFiles(){
- $i=0;
- foreach($_FILES as $file){
- if(is_string($file['name'])){ //單文件判定
- $files[$i]=$file;
- $i++;
- }elseif(is_array($file['name'])){
- foreach($file['name'] as $key=>$val){ //我的天,這個(gè)$key用的diao
- $files[$i]['name']=$file['name'][$key];
- $files[$i]['type']=$file['type'][$key];
- $files[$i]['tmp_name']=$file['tmp_name'][$key];
- $files[$i]['error']=$file['error'][$key];
- $files[$i]['size']=$file['size'][$key];
- $i++;
- }
- }
- }
- return $files;
- }
里面封裝了兩個(gè)小的
- function uploadFile($fileInfo,$path='./uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){
- //$flag=true;
- //$allowExt=array('jpeg','jpg','gif','png');
- //$maxSize=1048576;//1M
- //判斷錯(cuò)誤號(hào)
- $res=array();
- if($fileInfo['error']===UPLOAD_ERR_OK){
- //檢測上傳得到小
- if($fileInfo['size']>$maxSize){
- $res['mes']=$fileInfo['name'].'上傳文件過大';
- }
- $ext=getExt($fileInfo['name']);
- //檢測上傳文件的文件類型
- if(!in_array($ext,$allowExt)){
- $res['mes']=$fileInfo['name'].'非法文件類型';
- }
- //檢測是否是真實(shí)的圖片類型
- if($flag){
- if(!getimagesize($fileInfo['tmp_name'])){
- $res['mes']=$fileInfo['name'].'不是真實(shí)圖片類型';
- }
- }
- //檢測文件是否是通過HTTP POST上傳上來的
- if(!is_uploaded_file($fileInfo['tmp_name'])){
- $res['mes']=$fileInfo['name'].'文件不是通過HTTP POST方式上傳上來的';
- }
- if($res) return $res;
- //$path='./uploads';
- if(!file_exists($path)){
- mkdir($path,0777,true);
- chmod($path,0777);
- }
- $uniName=getUniName();
- $destination=$path.'/'.$uniName.'.'.$ext;
- if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){
- $res['mes']=$fileInfo['name'].'文件移動(dòng)失敗';
- }
- $res['mes']=$fileInfo['name'].'上傳成功';
- $res['dest']=$destination;
- return $res;
- }else{
- //匹配錯(cuò)誤信息
- switch ($fileInfo ['error']) {
- case 1 :
- $res['mes'] = '上傳文件超過了PHP配置文件中upload_max_filesize選項(xiàng)的值';
- break;
- case 2 :
- $res['mes'] = '超過了表單MAX_FILE_SIZE限制的大小';
- break;
- case 3 :
- $res['mes'] = '文件部分被上傳';
- break;
- case 4 :
- $res['mes'] = '沒有選擇上傳文件';
- break;
- case 6 :
- $res['mes'] = '沒有找到臨時(shí)目錄';
- break;
- case 7 :
- case 8 :
- $res['mes'] = '系統(tǒng)錯(cuò)誤';
- break;
- }
- return $res;
- }
- }
然后靜態(tài)中,用multiple屬性實(shí)現(xiàn)多個(gè)文件的輸入;
- function getExt($filename){
- return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
- }
- /**
- * 產(chǎn)生唯一字符串
- * @return string
- */
- function getUniName(){
- return md5(uniqid(microtime(true),true));
- }
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="doAction6.php" method="POST" enctype="multipart/form-data">
- 請選擇您要上傳的文件:<input type="file" name="myFile[]" multiple='multiple' /><br/>
- <input type="submit" value="上傳"/>
- </form>
- </body>
- </html>
- <?php
- //print_r($_FILES);
- header("content-type:text/html;charset=utf-8");
- require_once 'upFunc2.php';
- require_once 'common.func.php';
- $files=getFiles();
- // print_r($files);
- foreach($files as $fileInfo){
- $res=uploadFile($fileInfo);
- echo $res['mes'],'<br/>';
- $uploadFiles[]=@$res['dest'];
- }
- $uploadFiles=array_values(array_filter($uploadFiles));
- //print_r($uploadFiles);
通過以上的幾個(gè)文件,就能實(shí)現(xiàn)比較強(qiáng)大的面向過程的上傳文件的功能了。
分享:php按單詞截取字符串的方法這里指定字符串和單詞數(shù)量進(jìn)行截取 代碼如下:?php function limit_words($string, $word_limit) { $words = explode( ,$string); return implode( ,array_splice($words,0,$word_limit)); } //Example Usage $content = Lorem ipsum dolor sit amet, consectetur adipi
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php多文件上傳封裝
。