高人 指路。。
php怎么一个file上传多张图片?????????
- 提问者网友:骑士
- 2021-04-28 22:47
- 五星知识达人网友:摆渡翁
- 2021-04-28 23:00
<?php
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";
}
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群讨论!
- 1楼网友:一把行者刀
- 2021-04-29 03:16
- 2楼网友:孤独的牧羊人
- 2021-04-29 02:12
<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']; } } function set_filename($file='',$tmpfile) { $this->get_ext($tmpfile['name']); if(preg_match('/\,'jpg,gif,png'); //$upload->up_file('upload',$name); ?>
- 3楼网友:妄饮晩冬酒
- 2021-04-29 00:37
<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']接收
- 4楼网友:第幾種人
- 2021-04-28 23:06
一个file能多张上传吗?
上传都是多个