之前做的一个图片上传

 
<?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_
    }
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值