0

I have a problem to upload a .pdf file from modal to database. The problem show an error message "You did not select a file to upload." Please help me to solve my problem

my modal

<?php echo form_open_multipart('pengaduan/simpanberkas');?>
<form class="form-horizontal" action="" method="post" name="berkas" enctype="multipart/form-data" />
    <div class="form-group">
        <label class="col-lg-2 control-label">Berkas</label>
        <div class="col-lg-5">
            <input type="file" name="berkas" class="berkas" id="berkas" >
        </div>
            <label>Format berkas .PDF, .doc, .docx, atau .xls</label>
    </div>
    <div class="controls">
        <a href="<?php echo base_url();?>index.php/pengaduan/simpanberkas/<?php echo $key;?>"><button type="submit" name="tambah" id="tambah" class="btn btn-primary btn-small" value="tambah">Upload</button></a>      
    </div>
</form>

My controller

public function simpanberkas()
    {
        $key = $this->uri->segment(3);
        $this->load->model('model_pengaduan');
        $query = $this->model_pengaduan->getdata($key);
        if($query->num_rows>0)
        {
            foreach ($query->result() as $row) {
                $data['file'] = $row->file;
            }
        }

        $config['upload_path'] = './assets/file/';
        $config['allowed_types'] = 'pdf|doc|docx|excel';
        $config['max_size'] = '1000';
        //$config['max_width']  = '2000';
        //$config['max_height']  = '1024';
         $this->load->library('upload',$config);
         //$this->upload->initialize($config);
         //$berkas = $this->input->post('berkas');
            if(!$this->upload->do_upload('berkas')){
                    $error = array('error' => $this->upload->display_errors());
                    $this->session->set_flashdata('info',$error['error']);
            }else{
                    $file_data=$this->upload->data();
                    $data['file']=base_url().'/assets/file/'.$file_data['file_name'];
            }

            $this->model_pengaduan->getupdate($key,$data);
            redirect('pengaduan');      
    }

1 Answers1

0

Check with firebug if your form open shows ip in address then you might have trouble submitting form.

Make sure you have set your base url in config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/

$config['base_url'] = 'http://localhost/your_project/';

Form

<?php echo form_open_multipart('pengaduan/simpanberkas/' . $key);?>

<!-- form content -->

<button type="submit">Save</button>

<?php echo form_close();?>

This will send the $key value to your simpanberkas function you may need to set your routes for it

$route['pengaduan/simpanberkas'] = 'pengaduan/simpanberkas';
$route['pengaduan/simpanberkas/(:any)'] = 'pengaduan/simpanberkas/$1';

And then on the controller to get the key use uri segment

Controller: Your file and class names should only have first letter upper case only like

Filename: Pengaduan.php

<?php

class Pengaduan extends CI_Controller {

    public function simpanberkas() {

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

        $key = $this->uri->segment(3);
        $this->load->model('model_pengaduan');
        $query = $this->model_pengaduan->getdata($key);

        if($query->num_rows > 0) {

        foreach ($query->result() as $row) {
                $data['file'] = $row->file;
            }
        }

        $config['upload_path'] = './assets/file/';
        $config['allowed_types'] = 'pdf|doc|docx|excel';
        $config['max_size'] = '1000';

        //$config['max_width']  = '2000';
        //$config['max_height']  = '1024';

        $this->upload->initialize($config);

        if(!$this->upload->do_upload('berkas')){

            $error = array('error' => $this->upload->display_errors());
            $this->session->set_flashdata('info',$error['error']);

        } else {

            // Success part redirects stuff here

            $file_data=$this->upload->data();
            $data['file']=base_url().'/assets/file/'.$file_data['file_name'];

            this->model_pengaduan->getupdate($key,$data);
            redirect('pengaduan');      
        }

    }

}
  • In the config -> mimes.php have you added `'pdf' => array('application/pdf'),` –  Aug 09 '16 at 03:07
  • Nothing happend when I clicked the button – Ahmad Faza Aug 09 '16 at 03:10
  • Have you set your base url if not you need to. Have you you named your file Pengaduan.php and `class Pengaduan extends CI_Controller {}` with the first letter only upper case –  Aug 09 '16 at 03:12
  • I've done that, but the button still show nothing when i clicked – Ahmad Faza Aug 09 '16 at 03:15