4个回答
展开全部
<?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群讨论!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<input type='file' name=file[] size =22> <input type='file' name=file[] size =22> <input type='file' name=file[] size =22> <input type='file' name=file[] size =22> $_FILES['file']接收
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一个file能多张上传吗? 上传都是多个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<input type='file' name=file[] size =22> <input type='file' name=file[] size =22> <input type='file' name=file[] size =22> <input type='file' name=file[] size =22> $_FILES['file'][]//他的类型是一个数组。明白原理就简单了。 <?php class upLoad { var $savepath = 'F:/wamp/www/data/'; //file save dir var $saveurl = ' http://127.0.0.1/data/'; var $format = 'jpg,gif,png,tif,bmp'; //Establishment document extension var $maxsize = 5242880; //file size is 5M; var $ext; //extension var $filepath; //file path var $filename = ''; //file name var $fileinfo = array(); //info,他返回上传后的文件路径,URL,名称,大小,扩展名,方便引用 function upLoad($path='',$url='',$maxsize='',$format = '') { if($path != '') { $path = (substr($path, -1) == "/") ? $path : $path.'/'; $this->savepath = $path; } if($url != '') { $url = (substr($url,-1) == '/') ? $url : $url.'/'; $this->url = $url; } $this->maxsize = ($maxsize == '') ? $this->maxsize : $maxsize; $this->format = ($format == '') ? $this->format : $format; } function up_file($form,$filename='') { if(!isset($_FILES[$form])) { emMsg('没有文件可上传'); } if(!is_writable($this->savepath)) { emMsg('所指定的保存文件位置没有写的权限'); } if(is_array($_FILES[$form]['name'])) { for($i = 0; $i<count($_FILES[$form]); $i++) { $tmpinfo = array(); $tmpinfo['name'] = $_FILES[$form]['name'][$i]; $tmpinfo['tmp_name'] = $_FILES[$form]['tmp_name'][$i]; $tmpinfo['size'] = $_FILES[$form]['size'][$i]; $tmpinfo['error'] = $_FILES[$form]['error'][$i]; $this->set_filename('',$tmpinfo); $this->copyfile($tmpinfo); $this->fileinfo[$i]['url'] = $this->url; $this->fileinfo[$i]['name'] = $this->filename; $this->fileinfo[$i]['size'] = $tmpinfo['size']; $this->fileinfo[$i]['ext'] = $this->ext; $this->fileinfo[$i]['path'] = $this->filepath; } } else { $this->set_filename($filename,$_FILES[$form]); $this->copyfile($_FILES[$form]); $this->fileinfo[0]['url'] = $this->url; $this->fileinfo[0]['name'] = $this->filename; $this->fileinfo[0]['size'] = $_FILES[$form]['size']; $this->fileinfo[0]['ext'] = $this->ext; $this->fileinfo[0]['path'] = $this->filepath; } } function copyfile($file) { if($file["size"] > $this->maxsize) { emMsg('文件大小超过限制'); } if(!in_array(strtolower($this->ext), explode(",", strtolower($this->format)))) { emMsg('只能上传反缀名为'.$this->format.'的文件'); } if(is_uploaded_file($file["tmp_name"])) { if(file_exists($this->savepath)) { if(!move_uploaded_file($file["tmp_name"], $this->filepath)) { $errors = array( 0=>"File upload success", 1=>"Upload file Size exceeded the limit value system.", 2=>"Upload files over the size of the HTML form MAX_FILE_SIZE option to specify the value.", 3=>"Only some file were uploaded. ", 4=>"There were no file were uploaded. " ); emMsg($errors[$file["error"]]); } else { @unlink($file["tmp_name"]); } } elseif(!@mkdir($this->savepath,'0700')){ emMsg('所指定的上传目录不存在'); } elseif(file_exists($this->savepath)){ $this->copyfile($file); } } } function get_ext($f) { if($f != "") { $ext = pathinfo($f); $this->ext = $ext['extension']; } } /* * 可以合用命名模式 * 格式 /.../ * Y 年 * m 月 * d 日 * H 时 * i 分 * s 秒 * r 四位的随机数 * S 文件大小 * o 护展名 例:/Hmdr/返回200910282547 */ function set_filename($file='',$tmpfile) { $this->get_ext($tmpfile['name']); if(preg_match('/\/*\//',$file)) { $file = preg_replace('/\//','',$file); $file = preg_replace('/Y/',date('Y'),$file); $file = preg_replace('/m/',date('m'),$file); $file = preg_replace('/d/',date('d'),$file); $file = preg_replace('/H/',date('H'),$file); $file = preg_replace('/i/',date('i'),$file); $file = preg_replace('/s/',date('s'),$file); $file = preg_replace('/r/',mt_rand(1000,9999),$file); $file = preg_replace('/S/',$tmpfile['size'],$file); $file = preg_replace('/o/',preg_replace('/.'.$this->ext.'/','',$tmpfile['name']),$file); $this->filename = $file.'.'.$this->ext; } elseif($file != ''){ $this->filename = $file.'.'.$this->ext; } else{ $rand = mt_rand(1000,9999); $md = substr(md5($rand),mt_rand(0,32),4); $newfile = date("YmdHis").$md; $this->filename = $newfile.'.'.$this->ext; } $path = $this->savepath.$this->filename; if(file_exists($path)) { $this->set_filename('',$tmpfile); } else { $this->filepath = $this->savepath.$this->filename; $this->url = $this->url.$this->filename; } } } //$upload = new upLoad('F:/www/updata/',' http://127.0.0.1/updata','1024'/*maxSize*/,'jpg,gif,png' ); //$upload->up_file('upload'/*form*/,$name); ?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询