I am trying to implement a post functionality and want to pick message and image from a php view. My message functionality is working good. But on image upload i receive an error You did not select a file to upload. This is my controller function
function post_func()
{
    session_start();
    echo $post_message=$_POST['post'];
    echo $share_with=$_POST['share_with'];
    echo $image=$_POST['image'];
    if($image==null){
        echo "<br/>no image<br/>";  
    }
    else{
        ////////////////////////////////////////////////////////////////////////////////////////
            $config['upload_path'] = './application/css';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                echo "<br/>";
                echo $this->upload->display_errors();
                echo "<br/> image error<br/>";
            }
            else
            {
                echo "<br/> reached <br/>";
                session_start();
                $this->membership_model->insert_images($this->upload->data(),$email);
                $data = array('upload_data' => $this->upload->data());
                echo "<br/ problem<br/>";
            }
        ////////////////////////////////////////////////////////////////////////////////////////
    }
    $public;
    if($share_with=="public"){
        echo "1";
        $public=true;
    }else{
        echo "0";
        $public=false;
    }echo "-----------------------------<br/>";
    echo $user=$this->session->userdata('user_identification');
    $data = array 
            (
                'userid'=> $user,
                'public' => $public,
                'message' => $post_message,
                'picname' => "None"
            );
    $this->load->model('membership_model');
    $this->membership_model->add_message($data);
    echo "</br>";
    echo $user=$this->session->userdata('user_identification');
}
This is my view.
<?php echo form_open('search/post_func');?>
<!--<form id="loginForm" action="../search/post_func" method="post" enctype="multipart/form-data" >-->
<div id="your_post">
<div id="post_image">
<img   id ="post_img" src="<?php  echo $this->config->item('base_url'); ?><?php echo '/application/css/'. $img ?>"/>
</div>
<textarea name="post" rows="5" cols="30" placeholder="Share an update..." id="post_text" rows="2" value=""></textarea>
//other view items
            <?php
    echo form_close();
    ?> 
Please help me
Change:
$this->upload->do_upload()
To this:
$this->upload->do_upload('my_file_input_name')
and it will work!! :)
Change:
<?php echo form_open_multipart('search/post_func');?>
                        Your form type should be "multipart"
 Change your form tag to:  
 <?php echo form_open_multipart('search/post_func');?>
                        enctype='multipart/form-data'
you should put previous attribute in form tag
$this->upload->do_upload('file_name')
pass input tag file name to pervious line in your controller
<input type="file"   name ="file_name" >
                        example code is
    $config['upload_path'] = FCPATH.'upload/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $this->load->library('upload');
            $this->upload->initialize($config);  
    if ( ! $this->upload-> do_upload('userfile'))
    {
                   $errors = $this->upload->display_errors();
                   $this->session->set_flashdata('error', $errors);
                       redirect('ur_link');
    }
    else
    { 
                            $data = array('upload_data' => $this->upload->data());
                            $fullpath= $data['upload_data']['full_path'];
                            $file_name = $data['upload_data']['file_name'];
                    }
    }
                        Do this:
if(isset($_FILES['profile_pic']['name']) && !empty($_FILES['profile_pic']['name'])){
    $config['upload_path']= './assets/img/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 10000;
    $config['max_width'] = 10000;
    $config['max_height'] = 10000;
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('profile_pic'))
    {
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
        exit;
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        if(!empty($data)){
            $img1 =  $data['upload_data']['file_name'];                           
        }
    }
}
$insert_data['profile_pic'] = $img1;
                        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