CodeIgniter Forums

Full Version: Upload problem,newbie here please help :)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
how to insert upload form in my function?
controller file
Code:
class Hasil_rad extends Controller
{
function Hasil_rad()
    {
        parent::Controller();
              $this->load->helper(array('form', 'url'));
        if ($this->session->userdata('logged_in')==FALSE)
            redirect('main');
    }

function konfirmasi_tambah_hasil_rad($pasien_id, $r_id)
    {
        if($this->input->post('tambah'))
        {
                $this->load->model('hasil_pemeriksaan_rad_model');
                $data = array(
                    'pasien_id' => $pasien_id,
                    'hasil_rad_id' => $r_id,
                        'tanggal' => $_POST['tanggal'],
                        'jenis_rad_id' => $_POST['j_id'],
                        'hasil_rad_1' => $_POST['hasil_1'],
                        'hasil_rad_2' => $_POST['hasil_2'],
                        'hasil_rad_3' => $_POST['hasil_3'],
                        'hasil_rad_4' => $_POST['hasil_4'],
                        'hasil_rad_5' => $_POST['hasil_5'],
                    'hasil_analisis_rad' => $_POST['keterangan']
                    
                );
                $this->db->insert('hasil_rad', $data);
                redirect('hasil_pemeriksaan/index/'.$pasien_id);
            }
        
        
        else
        {
            redirect('hasil_pemeriksaan/index/'.$pasien_id);
        }
    }
}

function do_upload($file)
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->upload->initialize($config);
        $this->load->library('upload', $config);
        
        if ( ! $this->upload->do_upload('userfile'))
        {
            //fail
        }
        else
        {            
            $data = array('filename' => $this->upload->data());
        }

my view file

Code:
<?php $this->load->view('header'); ?>

<?php echo form_open_multipart('hasil_rad/do_upload');?>

<table width="546" border="0">
        <tr>
          <td>Hasil 3* </td>
          <td>:</td>
          <td>
            &lt;input type="text" name="hasil_3" size="40"&gt;
          </td>
        </tr>
        <tr>
          <td>Hasil 4* </td>
          <td>:</td>
          <td>
            &lt;input type="text" name="hasil_4" size="40"&gt;
          </td>
        </tr>
        <tr>
          <td>Hasil 5* </td>
          <td>:</td>
          <td>
            &lt;input type="text" name="hasil_5" size="40"&gt;
          </td>
<tr>
          <td>Gambar</td>
          <td>&nbsp;</td>
        
          <td>&lt;input type="file" name="userfile" /&gt;
           </td>
          <td>&lt;form id="form1" name="form1" method="post" &gt;
          </td>
      </tr>
</table>

i feel like i'm doing right,but the images were not uploaded to uploads right, any body can correct my codes?i can use upload form in CI user guide well, but i can't adapt those code to my view and controller file. my view file have 3 field and 1 upload form,and 1 save button. so if i click save button,then i upload image and i get content in all field. i can get content with $_POST, but the upload form doesn't work. why?

El Forum

[eluser]Sumon[/eluser]
without looking much i have few questions
1. do you create uploads folder in your root and set it write permission(0777)?
2. change function name function do_upload($file) by function do_upload(). i feel, you don't need any parameter because your form in view
Code:
&lt;?php echo form_open_multipart('hasil_rad/do_upload');?&gt;
which means form action = "hasil_rad/do_upload"
and let us know if the problem still exists.

El Forum

[eluser]umefarooq[/eluser]
another thing if you file size is more than 100kb than it also be not uploaded because in you config array you can check max size 100, change it to 1024 for 1Mb or 2048 for 2MB, maximum allowed is 2MB check the user guide also for that.

Code:
$config['max_size']    = '100';
change it to
$config['max_size']    = '1024'; 1MB
or
$config['max_size']    = '2048'; 2MB

hope it will work also and also check your upload folder path is it on your right path

El Forum

[eluser]Unknown[/eluser]
still don't work. i don't know what's wrong here. CI upload form can work well with me, but why when i just copied a piece of code from there (it's just one field of input type of file, and one line of form_open_multipart), it such moving a hundred of code in here.

form_open_multipart('upload/do_upload'), my explanatory is say, make form in class upload.php, run method do_upload, so i just renamed it to form_open_multipart('hasil_rad/do_upload') which means, make form in hasil_rad class, run method do_upload there.

my question is, where the variable is going?what form_open_multipart is doing there with field contain address path?

another question if i was in functionA() and wants to call do_upload from there, is it possible?

El Forum

[eluser]Sumon[/eluser]
have a look view source(in browser)page and look for
Code:
&lt;form action="classname/func_name" method="post"&gt;
and surely if your variable within &lt;form&gt; and &lt;/form&gt; then it goes into classname/func_name. you can get it as $_POST['file'] or something like it.

lastly, it's possible to use another function for file upload. from functionA() function, you may get it exactly as func_name function(i am not 100% sure) or you may pass file variable as function parameter(surely)

although, i can't recall my memory, but you may use &lt;form type="mu..." methos=".."&gt; instead of form_open_multipart('hasil_rad/do_upload') because there may be not huge advantage of later one over former one.