我的PHP常用代码段

/**
     *用户文件上传
     */
    public  function  userFile($file='', $exts=[]){
        $config = array(
            'maxSize' =>'3145728',// 设置附件上传大小
            'savePath' => './Users/',//设置保存路径
            'exts' =>$exts,// 设置附件上传类型
            'autoSub'=> true,//自动使用子目录保存上传文件 默认为true
            'subName' => array('date', 'Ymd'),//子目录创建方式,采用数组或者字符串方式定义
        );
        $upload = new \Think\Upload($config);//实例化上传类
        if(!$info = $upload->upload($file)){
            //if there isn't a file will send a error message.
            $res = [
                'status' => 0,
                'message'=>$upload->getError()
            ];
            $this->ajaxReturn($res);
        }
        $url=[];//it is a path to save the files
        foreach ($info as $k => $file){
            $url[$k] = str_ireplace('./','/Uploads/',$file['savepath'].$file['savename']);
        }
        $res = [
            'status' => 1,
            'url'    =>$url
        ];
        return $res;
    }

多表联合

Public function lst(){
        $news = M('News');
        $new = $news->limit(I('get.offset'),I('get.limit'))->select();
        $total = count($news->select());
        $n_type = M('news_type');
        foreach ($new as $key => $v) {
            $temp1 = $n_type->find($v['type']);
            $new[$key]['type'] = $temp1['name'];
        }
        $data=[
            'total' => $total,
            'rows'  =>$new
        ];
        //print_r($data1);exit();
        $this->ajaxReturn($data);
    }

后台进行修改操作时,没有修改图片,再次刷新图片地址为空?
需要在修改函数中加入一个去除空字符串的函数.

/**
 * 去除数组中的空字符串
 */
function removeEmpty($arr) {
    $array = $arr;
    foreach ($array as $key => $v) {
        if (trim($v) == '') {
            unset($array[$key]);
        }
    }
    return $array;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值