im trying to write a code where i can submit a form enter the contents in a database, at the same time perform a file upload and have it stored in a folder inside my server
the folder location is called uploads which is located at the root of my site
here is my code
controller (site.php)
 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 class Site extends CI_controller {
public function __construct()
{
    parent::__construct();
    // Your own constructor code
    $this->load->model("site_model");
    $this->load->helper(array('form', 'url'));
} 
public function cmain($type,$page)
{
    $data = $this->_initialize_data(); //this is just a bunch of variables that im calling in another function
    $data['type'] = $type;
    $data['page'] = $page;
    $this->load->vars($data);
    $this->load->view('site/cmain');
}
public function m1()
        {               
        $this->load->library('upload', $config);
            if(isset($_POST['m1']))
            {
                $suffix = $this->input->post("suffix");
                $fn = $this->input->post("fn");
                $mn = $this->input->post("mn");
                $ln = $this->input->post("ln");
                $newdata = array('suffix'=>$suffix,
                                'fn'=>$fn,
                                'mn'=>$mn,
                                'ln'=>$ln,
                                 );
                //this code is for the file upload
                           $config['upload_path'] = 'uploads';
                           $config['allowed_types'] = '*';
                           $this->load->library('upload', $config);
                           $data = array('upload_data' => $this->upload->data());
                 //end of file upload codes                 
                $this->db->insert('myself', $newdata); 
                redirect(base_url() . "site/complaint");    
            }
        }
view (cmain.php)
<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">
        <table class='table' width="100%">
            <tr>
            <td colspan='2'>
                <b><font color="#3B608C">Personal Information</font></b>
            </td>
        </tr>
        <tr>
            <td>
                Suffix  (e.g. Jr., III)
            </td>
            <td>
                <input type="text" name="suffix" id="suffix" value="">
            </td>
        </tr>
        <tr>
            <td>
                First Name*
            </td>
            <td>
                <input type="text" name="fn" id="fn" value="">
            </td>
        </tr>
        <tr>
            <td>
                Middle Name*
            </td>
            <td>
                <input type="text" name="mn" id="mn" value="">
            </td>
        </tr>
        <tr>
            <td>
                Last Name*
            </td>
            <td>
                <input type="text" name="ln" id="ln" value="">
            </td>
        </tr>
</table>
            <table>
        <tr>
        <td width="50%">
        Please attach documents pertinent to these complaints. <br>
        (Attach a zip file if more than one document)<br>
        </td>
        <td align="center">
        <input name = "userfile" type="file" class="input-xlarge" id = "userfile" />
        </td></tr>
        </table>
        <input type="submit" value="Submit Form" class="pull-right" id="submit" name="m1"/>
</form>
the form posts properly in the database like the suffix,fn,ln and mn however the file upload isnt working
i tried my best to follow what the codeigniter docs samples and only got the lines i think i needed
am i doing something wrong?
thanks
For Uploading files, as usually we need a Simple HTML form, with an input field and submit button. Here is the Code: 'file','name' => 'userfile')); echo form_submit('submit','upload'); echo form_close(); ?>
$this->upload->do_upload()Performs the upload based on the preferences you've set. Note: By default the upload routine expects the file to come from a form field called userfile , and the form must be a "multipart type: <form method="post" action="some_action" enctype="multipart/form-data" />
PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.
finally got it working
i used this for the file upload part
                //start of file upload code
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = '*';
                $this->load->library('upload', $config);
                $this->upload->do_upload();
                $data = $this->upload->data();
                //end of file upload code
and changed the
<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">
into
<?php echo form_open_multipart('site/c_upload');?>
                        I think you must have to set file upload preference
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
Form Helper
$this->load->helper(array('form', 'url'));
For further detail have a look at my blog post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With