使用codeigniter将图像上传并保存到数据库中的路径 [英] image upload and save path in database using codeigniter

查看:40
本文介绍了使用codeigniter将图像上传并保存到数据库中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有上传图片并将 image 路径保存在数据库中我的表名称是 uploadimage ,控制器名称是 upload.php

i did not get image upload and save image path in database My table name is uploadimage and controller name is upload.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {


    public function index()
    {
        $this->load->view('view');
    }
    public function do_upload()

    {
        $status="";
        $msg="";
        $filename='product_pic';
        if(empty($_POST['title']))
        {
            $status="error";
            $msg="plz enter title";
        }
        if($status!="error")
        {
            $config['upolad_path']='./file/';
             $config['allowed_types']='gip|jpg|png';
             $config['max_size']=1024*0;
             $config['encrypt_name']=true;
             $this->load->library('upload',$config);
             if(!$this->upload->do_upload($filename))
             {
                 $status='error';
                 $msg=$this->upload->display_errors('','');
             }
             else
             {
                 $this->load->model('file_model');
                 $data=$this->upload->data();
                 $file_id=$this->file_model->insert_file($data['file_name'],$_POST['title']);
                 if($file_id)
                 {
                     redirect('Upload/view');
                 } 
         }
    }

推荐答案

你需要这个

view.php

<?= form_open_multipart('upload/do_upload')?>
    <?= form_upload('userfile')?>
<?= form_close()?>

upload.php

upload.php

function do_upload(){
    $name = $_FILES["file"]["name"];
    $ext = end((explode(".", $name))); # extra () to prevent notice

    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = 0;

    $this->load->library('upload', $config);

    if( ! $this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else{
        $upload_data = $this->upload->data();

        #you can choose from
        /*
           Array(
                 [file_name]    => mypic.jpg
                 [file_type]    => image/jpeg
                 [file_path]    => /path/to/your/upload/
                 [full_path]    => /path/to/your/upload/jpg.jpg
                 [raw_name]     => mypic
                 [orig_name]    => mypic.jpg
                 [client_name]  => mypic.jpg
                 [file_ext]     => .jpg
                 [file_size]    => 22.2
                 [is_image]     => 1
                 [image_width]  => 800
                 [image_height] => 600
                 [image_type]   => jpeg
                 [image_size_str] => width="800" height="200"
          )
        */

        $this->model->insert_data($upoad_data['file_name'], $upoad_data['full_path']);

        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

model.php

model.php

function insert_data($name, $path_name){
    $data = array(
                  'name'    => $name,
                  'path'    => $path_name
                 );

    $this->db->insert('table', $data);

    return $this->db->insert_id();
}

这样,您上传文件,然后获取有关上传的数据并将其插入db

With that, you upload your file, then get the data about the upload and insert it in db

这篇关于使用codeigniter将图像上传并保存到数据库中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆