CI中上传图片功能的设置

1)找到config文件夹中的autoload.php将其中的   $autoload['helper'] = array('')   改为$autoload['helper'] = array('file');

2)创建一个类在开始的function __construct( )加入 $this->load->helper(array('form','url'));

例:  

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
    }

3)在view文件夹里面的视图页面和平时一样

<form enctype="multipart/form-data" method="post" action="<=?site_url('admin/add_ap_ok')?>">

</form>

4)要在数据发送过去的页面对应的函数中进行上传图片的配置,而不是在提交数据页面进行,

例:

数据在add_ap.php 填好后提交到  add_ap_ok.php 所以应该在add_ap_ok()函数中进行上传图片的配置,下面是源码

    function add_ap_ok()
    {
        $config['upload_path'] =   './upload/';            //这个路径很重要
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('cs_ap_img'))
        {
                echo $this->upload->display_errors();
        }
        else
        {
             $data['upload_data']=$this->upload->data();  //文件的一些信息
             $img=$data['upload_data']['file_name'];           //取得文件名
        }
        $this->load->Model('Madmin');
        $this->Madmin->insert_ap();
    }

5)在model里怎样得到图片的名称呢?我看了下upload类的源文件后来用 $img = $this->upload->file_name 试了下,还真成功了。

例:

    function insert_ap()
    {
        $img = $this->upload->file_name;
        $this->db->query("INSERT INTO ap(cs_ap_cate,cs_ap_img,cs_ap_title,cs_ap_know,cs_ap_solution)
            VALUES('$cate','$img','$title','$know','$solution')");
        return $this->db->affected_rows();
    }

这个图片上传问题纠结了我半天,现在终于解决了,给大家分享下
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值