當前位置:首頁 » 文件管理 » php多圖片上傳

php多圖片上傳

發布時間: 2023-01-03 11:56:12

php怎麼一個file上傳多張圖片

<?php /** *類說明: * 使用new關鍵字實像化類,類中有兩個公用方法, * 方法create_input創建表單按鍵,請在相應的表單處引用該方法就可創建上傳表單的input按鍵 * 方法get_upfile()用於處理上傳文件 * 該類由 游天小蝦 製作,網頁製作交流群:69574955 * **/ class upfile { private $name = 'filename';//input表單名 private $namecount = 2;//設置上傳文件的個數 private $type = array('jpg','jpeg','gif','png');//文件格式 private $size = '1024';//文件大小單位kb private $upname = '';//上傳文件信息 private $updir = 'upfile/'; private $movename = '';//移動後的文件名 private $uparrs = array();//多文件上傳數組 private $error_type =0;//文件上傳產生的錯誤 /** * 創建文件上傳的表單控制項 * */ public function create_input(){ if(floor($this->namecount) == 1){ $input = "<p><input type='file' id=".$this->name." name=".$this->name."></p>"; }else{ for($i=0;$i<($this->namecount);$i++){ $input .= "<p><input type='file' id='".$this->name."[]' name='".$this->name."[]'></p>"; } } echo "$input"; } /** * 初始文件信息$file = $_FILES['file']['tem_name'] * **/ private function get_part(){ if($this->namecount == 1){ //判斷是否是多文件上傳 if($_FILES[$this->name]['tmp_name']){ $this->upname = $_FILES[$this->name]; }else{ $this->error_type += 100; //文件信息錯誤觀點 100; } }else{ if($_FILES[$this->name]){ $this->uparrs = $this->more_updata($_FILES[$this->name],$this->namecount);//對$_FILES取得的文件上信息重寫 }else{ $this->error_type += 100; //文件信息錯誤觀點 100; } } } /** * 多文件上傳時,數組重寫 * **/ private function more_updata($arrs,$num){ for($i=0;$i<$num;$i++){ $data[] =array('name'=>$arrs[name][$i],'type'=>$arrs[type][$i],'tmp_name'=>$arrs[tmp_name][$i],'error'=>$arrs['error'][$i],'size'=>$arrs['size'][$i]); } return $data; } /** * 判斷上傳文件大小 * **/ private function chck_size(){ if($this->upname['size']*1000 < $this->size){ $this->error_type += 300; //文件信息錯誤觀點 300; } } /** * 判斷上傳文件的類型 * **/ private function chck_type(){ if(!in_array($this->get_suffix($this->upname['name']),$this->type)){ $this->error_type += 500; //文件信息錯誤觀點 500; } } /** * 格式化上傳後的文件名 * **/ private function chck_name(){ $this->movename = date(Ymd).substr(md5(rand(0,date(Hms))),0,6)."."; $this->movename .= $this->get_suffix($this->upname['name']); } /** * 移動文件 * **/ private function move_file(){ if($this->updir){ if(!move_uploaded_file($this->upname['tmp_name'],$this->updir.$this->movename)){ $this->error_type += 700; //文件信息錯誤觀點 700; } }else{ mkdir($this->updir,"w"); chmod($this->updir,777); if(!move_uploaded_file($this->upname['tmp_name'],$this->updir.$this->movename)){ $this->error_type += 700; //文件信息錯誤觀點 700; } } } /** * 取得文件的後綴名 * **/ private function get_suffix($filename){//取得文件後綴名 $part = pathinfo($filename); $suffix = $part['extension']; return $suffix; } /** * 文件上傳處理 * **/ public function get_upfile() {//主上傳方法 if(floor($this->namecount) == 1){ $this->get_part(); $this->chck_name(); $this->chck_type(); $this->chck_size(); if($this->error_type ==0){$this->move_file();} if($this->error_type ==0){ echo "$this->movename 上傳成功 <br>"; }else{ echo "$this->movename 上傳失敗,錯誤: $this->error_type <br>"; $this->error_type=0; }; }else{ $this->get_part(); for($i=0;$i<floor($this->namecount);$i++){ $this->upname = ($this->uparrs[$i]); $this->chck_name(); $this->chck_type(); $this->chck_size(); if($this->error_type ==0){$this->move_file();} if($this->error_type ==0){ echo "$this->movename 上傳成功 <br>"; }else{ echo "$this->movename 上傳失敗,錯誤: $this->error_type <br>"; $this->error_type=0; }; } } } } $up = new upfile(); if($_POST['t1']){ $up->get_upfile(); } ?> <form name='f1' enctype = multipart/form-data action="" method="post"> <input type='text' name='t1'><br> <?php $up->create_input(); ?> <input type='submit' value='上傳'> </from> 剛不久寫的一個文件上傳的類!上面已經有說明了,你參考一下,不明白的話,可以問我,或者加入我們的QQ群討論!

㈡ ThinkPHP中圖片最多一次上傳20張,怎麼上傳更多圖片

$User->fenxiang_fm = 'Uploads/'.$info[0]["savename"]; // 這里的$info[0]["savename"]的下標[0]表示上傳的第1個圖片按順序,記住是下標
$User->fenxiang_sc = 'Uploads/'.$info[1]["savename"]; // 這里的$info[1]["savename"]的下標[1]表示上傳的第2個圖片按順序,記住是下標

function upload(){

import("ORG.Net.UploadFile");

$upload = new UploadFile();// 實例化上傳類

$upload->maxSize = 3145728 ;// 設置附件上傳大小

$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型

$upload->saveRule = time;//這里的時間是根據上傳的圖片的多少來自動改變圖片的名稱的(並且時間都不同,所以上傳的圖片的名稱就不會相同)

熱點內容
phpbaidu 發布:2025-08-27 03:31:32 瀏覽:659
rip壓縮 發布:2025-08-27 03:16:27 瀏覽:211
紳士圖書館的解壓碼 發布:2025-08-27 03:12:38 瀏覽:279
明日之後怎麼改伺服器地址 發布:2025-08-27 03:00:58 瀏覽:613
dev編譯技巧 發布:2025-08-27 02:59:40 瀏覽:128
二手安卓蘋果哪個劃算 發布:2025-08-27 02:48:04 瀏覽:281
vps搭建pptp伺服器 發布:2025-08-27 02:48:04 瀏覽:390
大容量存儲器推薦 發布:2025-08-27 02:47:31 瀏覽:510
cf怎麼擠伺服器 發布:2025-08-27 02:39:03 瀏覽:685
db2資料庫字元集 發布:2025-08-27 02:18:58 瀏覽:369