简体   繁体   中英

codeigniter how to upload image in join table

Okey, so i have search and try everything from google but i'm stuck. i'm trying to use upload feature on my existing CodeIgniter project. I want to upload them and then show them on my view with 64x64 px size, while the real size are 320x320 px.

I joined two table to get the 'name' field from the other table so i can show them on my add data view when i need to add new data, and i manage to do that, but i'm confused about how to put the upload code/function cause i have this 'add_data' function that i use to save what i post in 'tambah_mahasiswa' view to the database. I've tried to combine the 'do_upload' function on 'add_data' function but it's still not working. How do i solve this ?

This is my photo field on tb_mahasiswa table :

Field   Type        Attributes
photo   blob        BINARY

And here's my controller code = 'controlpanel' :

class Controlpanel extends CI_Controller
{
    private $path_uploads;
    private $path_uploads_thumb;
    function __construct()
    {
        parent::__construct();
                $this->cek_login();
        $this->load->model('modelku');

        $this->path_uploads = realpath( APPPATH . '../uploads/');
        $this->path_uploads_thumb = APPPATH . '../uploads/thumbnail';
    }

function add_data()
    {
        $this->load->library('form_validation');
        $this->load->helper('url');

        $data['prodi']=$this->modelku->get_prodi_all();

        $this->form_validation->set_rules('nim','Nomor Induk Mahasiswa','required|exact_length[7]');
        $this->form_validation->set_rules('nim','Nomor Induk Mahasiswa','required|integer');
        $this->form_validation->set_rules('nama','Nama Lengkap Mahasiswa','required');
        $this->form_validation->set_rules('prodi','Prodi','required');
        $this->form_validation->set_rules('tanggal_lahir','Tanggal lahir','required');
        $this->form_validation->set_rules('email','Email','required|valid_email');
        $this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
        $this->form_validation->set_rules('alamat','Alamat','required');
        $this->form_validation->set_rules('telepon','Telepon','required');

        if($this->form_validation->run())
        {
                        $nim = $this->input->post('nim');
                        $nama = $this->input->post('nama');
                        $prodi = $this->input->post('prodi');
                        $tanggal_lahir = date('Y-m-d',strtotime($this->input->post('tanggal_lahir')));
                        $email = $this->input->post('email');
                        $jenis_kelamin = $this->input->post('jenis_kelamin');
                        $alamat = $this->input->post('alamat');
                        $telepon = $this->input->post('telepon');
                        $photo = $this->input->post('photo');

                        $data_mahasiswa = array('nim'=>$nim,'nama'=>$nama,'prodi'=>$prodi, 'tanggal_lahir'=>$tanggal_lahir,
                                                'email'=>$email, 'jenis_kelamin'=>$jenis_kelamin,'alamat'=>$alamat,
                                                'telepon'=>$telepon,'photo'=>$photo);
                        ('controlpanel/index');
                } else {
            $this->load->view("tambah_mahasiswa",$data);
            }
    }

function do_upload()
    {
        $config['upload_path'] =  $this->path_uploads;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

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

            $this->load->view('tambah_mahasiswa', $error);
        }
        else
        {
            $upload_data = $this->upload->data();
            $this->load->view('controlpanel_view', $data);
            $file_name = $upload_data['file_name'];
            $config['image_library']='gd2';
            $config['source_image']=$this->path_uploads . '\\' . $file_name;
            $config['new_image']=$this->path_uploads_thumb . $file_name;
            $config['create_thumb']=TRUE;
            $config['maintain_ratio']=TRUE;
            $config['width']=320;
            $config['height']=320;

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

            if(!$this->image_lib->resize())
            {
                echo $this->image_lib->display_errors();
            } else {
                echo "Berhasil di unggah!";
            }
        }
    }

And this is my view = 'tambah_mahasiswa' :

<!DOCTYPE html>
<html>
<head>
<title>PHP Framework</title>
    <link rel="stylesheet" href="<?php echo base_url() ?>asset/css/style.css"/>
</head>
<body>

<h3>Tambah Data Mahasiswa</h3>

    <?php echo form_open('controlpanel/add_data'); ?> 
    <?php echo form_open_multipart('controlpanel/do_upload'); ?> 

<table width="30%" border="0">
    <tr>
    <td>NIM <sup>*</sup></td>
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'nim', 'maxlenght'=>'7', 'size'=>'10'),set_value('nim'))?> <?php echo form_error('nim','<span style="color:red;"><small>','</div></span>') ?> </br> 
        </td>
    </tr>

    <tr>
    <td>Nama <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'nama', 'maxlenght'=>'35', 'size'=>'15'),set_value('nama'))?> <?php echo form_error('nama','<span style="color:red;"><small>','</div></span>') ?> </br>
        </td>
    </tr>


        <td>Prodi <sup>*</sup> </td> 
    <td>:</td>
    <td>
        <?php
                    foreach($prodi as $item_prodi)
                    {
                        $array_prodi[$item_prodi->kode]=$item_prodi->namaprodi;
                    }
                echo form_dropdown('prodi',$array_prodi);
                ?>
    </td>
    </tr>

    <tr>
    <td width='30%'>Tanggal Lahir <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'tanggal_lahir', 'maxlenght'=> '10', 'placeholder'=>'yyyy-mm-dd', 'size'=>'15', 'id'=>'tanggal_lahir'),set_value('tgl_lahir'))?> <?php echo form_error('tgl_lahir','<span style="color:red;"><small>','</small></span>') ?>
        </td>
    </tr>

    <tr>
    <td>Jenis Kelamin <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_radio('jenis_kelamin', 'L', TRUE)?> Laki-laki
            <?php echo form_radio('jenis_kelamin', 'P')?> Perempuan
        </td>
    </tr>

    <tr>
    <td>Alamat <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_textarea(array('name'=>'alamat','rows'=>'10','cols'=>'30'),set_value('alamat'))?> <?php echo form_error('alamat','<span style="color:red;"><small>','</small></span>') ?> </br>
        </td>

    </tr>

    <tr>
    <td>Propinsi </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'propinsi', 'maxlenght'=>'15', 'size'=>'17','id'=>'propinsi'))?> <?php echo form_error('propinsi','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>No. Telp </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'telepon', 'maxlenght'=>'15', 'size'=>'17'))?> <?php echo form_error('telp','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>Email <sup>*</sup> </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'email', 'maxlenght'=>'30','placeholder'=>'name@example.com', 'size'=>'17'),set_value('email'))?> <?php echo form_error('email','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>Photo </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'photo', 'type'=>'file', 'size'=>'20'),set_value('photo')) ?>
        </br>
    </td>
    </tr>

    <tr>
    <td>
        <?php echo form_submit(array('type'=>"submit", 'name'=>"mysubmit", 'value'=>"Save"))?>
    </td>
    </tr>

    <?php echo form_close();?>

</table>
</body>
</html>

PS : i've made folders for image file on my project :

 - myproject\uploads\thumbnail

I could see from the above given code that in the controller class 'Controlpanel', you have defined a function do_upload().

But, inside the same controller function do_upload, you are calling the function with different class name 'upload' which is a library class.

$this->upload->do_upload()

Either you need to change the controller class name 'Controlpanel' to 'upload' or you can try changing the name of do_upload function in your current controller class to some other name then you can proceed as per your current code.

You should also make sure that the form name on the view page should be same as the form name inside the do_upload function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM