前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >我的PHP常用代码段

我的PHP常用代码段

作者头像
仇诺伊
发布2018-09-12 14:25:42
8770
发布2018-09-12 14:25:42
举报
文章被收录于专栏:佳爷的后花媛佳爷的后花媛
代码语言:javascript
复制
/**
     *用户文件上传
     */
    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;
    }

多表联合

代码语言:javascript
复制
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);
    }

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

代码语言:javascript
复制
/**
 * 去除数组中的空字符串
 */
function removeEmpty($arr) {
    $array = $arr;
    foreach ($array as $key => $v) {
        if (trim($v) == '') {
            unset($array[$key]);
        }
    }
    return $array;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.01.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档