当前位置:首页 » 文件管理 » 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;//这里的时间是根据上传的图片的多少来自动改变图片的名称的(并且时间都不同,所以上传的图片的名称就不会相同)

热点内容
学生选修课程系统设计c语言 发布:2025-08-27 00:29:27 浏览:72
盗版我的世界服务器换皮肤 发布:2025-08-27 00:28:19 浏览:367
数据库容灾 发布:2025-08-27 00:23:24 浏览:619
安装的联通宽带在哪里设置密码 发布:2025-08-27 00:13:43 浏览:476
视频程序源码 发布:2025-08-27 00:12:32 浏览:695
阿里云服务器优化 发布:2025-08-27 00:11:37 浏览:347
我的世界1710服务器小游戏 发布:2025-08-27 00:09:48 浏览:714
java去掉重复 发布:2025-08-27 00:09:47 浏览:675
多点机房搭建服务器集群 发布:2025-08-27 00:09:02 浏览:695
rc脚本编写 发布:2025-08-27 00:07:57 浏览:975