• thinkPHP框架之使用篇三


    一、Id  隐藏
    <p>
      <input class="text-input small-input" type="hidden" id="id" name="id" value="{$result.id}"/>
    </p>
     

    二、重写父类方法
    public $model = null;
    // (重写)框架的构造方法,运行工程时框架内部会执行,作用是初始化常用属性, 方法等初始化工作
    public function __construct()
    {
        // 调用父类方法
        parent::__construct();
        $this->model = M('Product');
    }
    完善后
    PublicAction
    继承父类
    构造函数
    class PublicAction extends Action {
        public $model = null;
        //框架的构造方法,运行工程师框架内部会执行,作用是初始化常用的属性,方法等初始化工作
        public function __construct()
        {
            //调用父类方法
            parent::__construct();
            //获取当前子类的名;
            $class_name = get_class($this);
            $name = substr($class_name,0,-6);
    //        dump($name);exit();
            $this->model = M($name);
        }
    }

    三、上传文件
    文件上传两大核心:
    <form action="__APP__/product/update" method="post" enctype="multipart/form-data> 支持文件上传
    <input class="text-input medium-input" type="file" id="" name="img" /> 文件类型
    查手册—》文件上传
    // 文件上传
    import('ORG.Net.UploadFile');
    $upload = new UploadFile();// 实例化上传类
    $upload->maxSize  = 3145728 ;// 设置附件上传大小
    $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->savePath =  './Public/Uploads/';// 设置附件上传目录
    if(!$upload->upload()) {// 上传错误提示错误信息 CMD进入upload()方法
        $this->error($upload->getErrorMsg());
    }else{// 上传成功 获取上传文件信息
        $info =  $upload->getUploadFileInfo();
    }
    // 保存表单数据 包括附件数据
    $User = M("User"); // 实例化User对象
    $User->create(); // 创建数据对象
    $User->photo = $info[0]['savename']; // 保存上传的照片根据需要自行组装
    $User->add(); // 写入用户数据到数据库
    $this->success('数据保存成功!');
     
    Mac 系统下修改文件权限,可以自动创建文件
    // 尝试创建目录      权限  
    if(!mkdir($savePath,777,true)){
    文件修改权限用Linux修改
    sudo chmod -R 777 /Applications/XAMPP/xamppfiles/htdocs/phprise/Public 
    // 对上传文件按日期建文件夹分类
    $year = date('Y');
    $month = date('M');
    $day = date('d');
    $upload->savePath =  './Public/Uploads/'.$year.'/'.$month.'/'.$day.'/';// 设置附件上传目录
    // 生成缩略图
    $upload->thumb = true;
    $upload->thumbMaxWidth = '50,200';
    $upload->thumbMaxHeight = '50,200';
    整体:
    if (!empty($_FILES['img']['name'])){
                // 文件上传
                import('ORG.Net.UploadFile');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 3145728 ;// 设置附件上传大小
                $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $year = date('Y');
                $month = date('M');
                $day = date('d');
                $upload->savePath =  './Public/Uploads/'.$year.'/'.$month.'/'.$day.'/';// 设置附件上传目录
                // 生成缩略图
                $upload->thumb = true;
                $upload->thumbMaxWidth = '50,200';
                $upload->thumbMaxHeight = '50,200';
                if(!$upload->upload()) {// 上传错误提示错误信息
                    $this->error($upload->getErrorMsg());
                }else{// 上传成功 获取上传文件信息
                    $info =  $upload->getUploadFileInfo();
    //                dump($info);exit();
                    // 保存到数据库中
                    // 拼接图片路径
                    $img_path = $info[0]['savepath'].$info[0]['savename'];
    //                dump($img_path);exit();
                    $_POST['img_path'] = $img_path;
                }
            }

    四、<!--指定当前的默认路径—> 在<head></head>标签内些
    <base href="__ROOT__/"/>
     

    五、模板整体拼接新方法 代替  
    public function index(){
    //       $this->view();
            // 获取当前方法的Tpl文件内的模板
            $result = $this->fetch();
            $this->assign('result', $result);
    //        dump($result);exit();
            $this->display('Public:layout');
        }
    代替 view()
    public function view(){
        $this->display('Public:header');
        $this->display();
        $this->display('Public:right');
        $this->display('Public:footer');
    }

    六、 正则表达式
    //    var reg1 = /^[a-zA-Zd]{1}w+@w+(.(com|net))?.(cn|com)$/; // 实体
     
     
  • 相关阅读:
    C库中与时间相关的函数
    读书笔记之: 程序员的自我修养——链接,装载与库(第1,2部分)
    STL中常用的一些算法
    C++ STL中的迭代器技术
    程序员面试宝典2(数据结构与算法)
    C/C++程序员面试宝典2
    读书笔记之: 数据库系统概论(第4版)
    C库中重要字符串函数的简要分析及实例
    程序员求职成功路(3)
    C库中对函数的可变参数的支持
  • 原文地址:https://www.cnblogs.com/z-han49888/p/6163213.html
Copyright © 2020-2023  润新知