<?php //图片删除函数 function img_del($type){ $data['type']=$type; $img_id=$_POST['img_id']; $img_num=count($img_id); $all_id=implode(",", $_POST['img_id']); $result=M()->query("select path from img_upload where id in (".$all_id.")"); //删除图片 for($i=0;$i<$img_num;$i++){ $filename=$result[$i]['path'];//图片路径 $thumb_filename =explode('/',$filename); $thumb_filename=$thumb_filename[0].'/'.$thumb_filename[1].'/'.$thumb_filename[2].'/'.'thumb_'.$thumb_filename[3];//缩略图路径 //检查图片文件是否存在 if(file_exists($filename) && file_exists($thumb_filename)) { unlink($filename); unlink($thumb_filename); } } //删除数据库记录 for($i=0;$i<$img_num;$i++){ $data['id']=$img_id[$i]; M('ImgUpload')->where($data)->delete(); } return $img_num; } //图片上传函数 function img_upload($type){ //获取图片尺寸,用于截取 $arr=getimagesize($_FILES['img_upload']['tmp_name']); $width=$arr[0]; $height=$arr[1]; import('ORG.Net.UploadFile'); //拼接图片名,防止重复 $_FILES['img_upload']['name']=time()."_".$_FILES['img_upload']['name']; $upload=new UploadFile(); $upload->maxSize = 3145728 ;//大小不能超过3M $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $savePath=$upload->savePath = './Public/img_upload/';// 设置附件上传目录 //生成缩略图 $upload->thumb = true; $upload->thumbPath='./Public/img_upload/'; $upload->thumbPrefix = 'thumb_'; if($width>$height) { $upload->thumbMaxWidth = '9999';//这两个参数都要设置 $upload->thumbMaxHeight = '85'; }else{ $upload->thumbMaxHeight = '9999';//这两个参数都要设置 $upload->thumbMaxWidth = '85'; } if(!$upload->upload()) { // 上传错误提示错误信息 $error=$upload->getErrorMsg(); } else{ $pid=$_POST['id']; $create_time=time(); $path=$savePath.$_FILES['img_upload']['name']; $name=$_POST['img_name']; $mem=$_POST['img_mem']; $img_msg['pid']=$pid; $img_msg['create_time']=$create_time; $img_msg['path']=$path; $img_msg['name']=$name; $img_msg['mem']=$mem; $img_msg['type']=$type; //开始存入数据库 return M('ImgUpload')->add($img_msg); //数据库只存原图路径,调用缩略图直接加前缀thumb_ } } ?>
之前做的一个图片上传
最新推荐文章于 2025-04-03 16:27:15 发布