Note that there are some explanatory texts on larger screens.

plurals
  1. POcodeigniter how to upload image in join table
    primarykey
    data
    text
    <p>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.</p> <p>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 ?</p> <p>This is my photo field on tb_mahasiswa table : </p> <pre><code>Field Type Attributes photo blob BINARY </code></pre> <p>And here's my controller code = 'controlpanel' :</p> <pre><code>class Controlpanel extends CI_Controller { private $path_uploads; private $path_uploads_thumb; function __construct() { parent::__construct(); $this-&gt;cek_login(); $this-&gt;load-&gt;model('modelku'); $this-&gt;path_uploads = realpath( APPPATH . '../uploads/'); $this-&gt;path_uploads_thumb = APPPATH . '../uploads/thumbnail'; } function add_data() { $this-&gt;load-&gt;library('form_validation'); $this-&gt;load-&gt;helper('url'); $data['prodi']=$this-&gt;modelku-&gt;get_prodi_all(); $this-&gt;form_validation-&gt;set_rules('nim','Nomor Induk Mahasiswa','required|exact_length[7]'); $this-&gt;form_validation-&gt;set_rules('nim','Nomor Induk Mahasiswa','required|integer'); $this-&gt;form_validation-&gt;set_rules('nama','Nama Lengkap Mahasiswa','required'); $this-&gt;form_validation-&gt;set_rules('prodi','Prodi','required'); $this-&gt;form_validation-&gt;set_rules('tanggal_lahir','Tanggal lahir','required'); $this-&gt;form_validation-&gt;set_rules('email','Email','required|valid_email'); $this-&gt;form_validation-&gt;set_rules('jenis_kelamin','Jenis Kelamin','required'); $this-&gt;form_validation-&gt;set_rules('alamat','Alamat','required'); $this-&gt;form_validation-&gt;set_rules('telepon','Telepon','required'); if($this-&gt;form_validation-&gt;run()) { $nim = $this-&gt;input-&gt;post('nim'); $nama = $this-&gt;input-&gt;post('nama'); $prodi = $this-&gt;input-&gt;post('prodi'); $tanggal_lahir = date('Y-m-d',strtotime($this-&gt;input-&gt;post('tanggal_lahir'))); $email = $this-&gt;input-&gt;post('email'); $jenis_kelamin = $this-&gt;input-&gt;post('jenis_kelamin'); $alamat = $this-&gt;input-&gt;post('alamat'); $telepon = $this-&gt;input-&gt;post('telepon'); $photo = $this-&gt;input-&gt;post('photo'); $data_mahasiswa = array('nim'=&gt;$nim,'nama'=&gt;$nama,'prodi'=&gt;$prodi, 'tanggal_lahir'=&gt;$tanggal_lahir, 'email'=&gt;$email, 'jenis_kelamin'=&gt;$jenis_kelamin,'alamat'=&gt;$alamat, 'telepon'=&gt;$telepon,'photo'=&gt;$photo); ('controlpanel/index'); } else { $this-&gt;load-&gt;view("tambah_mahasiswa",$data); } } function do_upload() { $config['upload_path'] = $this-&gt;path_uploads; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this-&gt;load-&gt;library('upload', $config); if ( ! $this-&gt;upload-&gt;do_upload()) { $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors()); $this-&gt;load-&gt;view('tambah_mahasiswa', $error); } else { $upload_data = $this-&gt;upload-&gt;data(); $this-&gt;load-&gt;view('controlpanel_view', $data); $file_name = $upload_data['file_name']; $config['image_library']='gd2'; $config['source_image']=$this-&gt;path_uploads . '\\' . $file_name; $config['new_image']=$this-&gt;path_uploads_thumb . $file_name; $config['create_thumb']=TRUE; $config['maintain_ratio']=TRUE; $config['width']=320; $config['height']=320; $this-&gt;load-&gt;library('image_lib',$config); if(!$this-&gt;image_lib-&gt;resize()) { echo $this-&gt;image_lib-&gt;display_errors(); } else { echo "Berhasil di unggah!"; } } } </code></pre> <p>And this is my view = 'tambah_mahasiswa' :</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;PHP Framework&lt;/title&gt; &lt;link rel="stylesheet" href="&lt;?php echo base_url() ?&gt;asset/css/style.css"/&gt; &lt;/head&gt; &lt;body&gt; &lt;h3&gt;Tambah Data Mahasiswa&lt;/h3&gt; &lt;?php echo form_open('controlpanel/add_data'); ?&gt; &lt;?php echo form_open_multipart('controlpanel/do_upload'); ?&gt; &lt;table width="30%" border="0"&gt; &lt;tr&gt; &lt;td&gt;NIM &lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'nim', 'maxlenght'=&gt;'7', 'size'=&gt;'10'),set_value('nim'))?&gt; &lt;?php echo form_error('nim','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/div&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Nama &lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'nama', 'maxlenght'=&gt;'35', 'size'=&gt;'15'),set_value('nama'))?&gt; &lt;?php echo form_error('nama','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/div&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;td&gt;Prodi &lt;sup&gt;*&lt;/sup&gt; &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php foreach($prodi as $item_prodi) { $array_prodi[$item_prodi-&gt;kode]=$item_prodi-&gt;namaprodi; } echo form_dropdown('prodi',$array_prodi); ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width='30%'&gt;Tanggal Lahir &lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'tanggal_lahir', 'maxlenght'=&gt; '10', 'placeholder'=&gt;'yyyy-mm-dd', 'size'=&gt;'15', 'id'=&gt;'tanggal_lahir'),set_value('tgl_lahir'))?&gt; &lt;?php echo form_error('tgl_lahir','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/small&gt;&lt;/span&gt;') ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Jenis Kelamin &lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_radio('jenis_kelamin', 'L', TRUE)?&gt; Laki-laki &lt;?php echo form_radio('jenis_kelamin', 'P')?&gt; Perempuan &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Alamat &lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_textarea(array('name'=&gt;'alamat','rows'=&gt;'10','cols'=&gt;'30'),set_value('alamat'))?&gt; &lt;?php echo form_error('alamat','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/small&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Propinsi &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'propinsi', 'maxlenght'=&gt;'15', 'size'=&gt;'17','id'=&gt;'propinsi'))?&gt; &lt;?php echo form_error('propinsi','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/small&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;No. Telp &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'telepon', 'maxlenght'=&gt;'15', 'size'=&gt;'17'))?&gt; &lt;?php echo form_error('telp','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/small&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Email &lt;sup&gt;*&lt;/sup&gt; &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'email', 'maxlenght'=&gt;'30','placeholder'=&gt;'name@example.com', 'size'=&gt;'17'),set_value('email'))?&gt; &lt;?php echo form_error('email','&lt;span style="color:red;"&gt;&lt;small&gt;','&lt;/small&gt;&lt;/span&gt;') ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Photo &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt; &lt;?php echo form_input(array('name'=&gt;'photo', 'type'=&gt;'file', 'size'=&gt;'20'),set_value('photo')) ?&gt; &lt;/br&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;?php echo form_submit(array('type'=&gt;"submit", 'name'=&gt;"mysubmit", 'value'=&gt;"Save"))?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php echo form_close();?&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>PS : i've made folders for image file on my project : </p> <pre><code> - myproject\uploads\thumbnail </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload