0

Hello I want to save the image to the database.. I written some code but it is not working fine. my code for controller is

function UploadImageView()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);

        $imgdata['file']=$_FILES;
        $this->load->model('Users');
        /*$response=$this->Users->Image_upload();
        header('Content-type: application/json');
        echo json_encode($response);
   */

        foreach($_FILES as $key => $value)
        {

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


            if ( !$this->upload->do_upload($key))
            {
                //PARSE ERRORS
                $error = array('error' => $this->upload->display_errors());

                        //var_dump($error);
                        $msg = $this->upload->display_errors('<p>', '</p>');
                        echo 'errore: '.$msg;


            }
            else
            {
               $this->load->model('Users');
               $this->Users->Image_upload($key);
            }
        }
    }

and my code for the model is as follows.

function Image_upload($value)
{
    $this->filedata=$value;
    $handle = fopen($this->filedata,"rb");
    $img =fread($handle, filesize('$filedata'));
    fclose($handle);
    $img = base64_encode($img);
    $data=array(
        'image'=>$img
    );
    $flag=$this->db->insert('testimage',$data);

    if($flag)
            {
                $this->db->where('image', $this->filedata);
                $query = $this->db->get('testimage');

                if ($query->num_rows() == 0)
                {
                    $response['status'] = false;
                    $response['userId'] = -1;           
                    $response['message'] = "Upload Failed!";
                }
                else
                {
                    $result = $query->result();
                    $response['status'] = true;
                    $response['file'] = $this->filedata;
                    $response['message'] = "Success!";      
                }   
            }
return $response;           
}

It returns error that "fopen(Imgupload): failed to open stream: No such file or directory"

and Message: filesize(): stat failed for $filedata and fread() expects parameter 1 to be resource, boolean given etc etc so anybody help me to save image in my database.

Nilesh Mahajan
  • 607
  • 4
  • 16

1 Answers1

1
if ( ! $this->upload->do_upload())
{
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
}
else
{
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('upload_success', $data);
}

Straight from the docs.

If you notice, they are sending the upload data to the view. So send it to the model instead:

$this->Users->Image_upload($this->upload->data());

Then your data is available in the array as is also explained in the docs:

$this->upload->data()

This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

So you can access the data in the model like so:

function Image_upload($data)
{
    $handle = fopen($data['full_path'],'rb');
    ...
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • Thanx.. this is working..I saved image in database in encoded format. Do You have any idea to display this encoded image on view page.. – Nilesh Mahajan Apr 10 '13 at 05:42
  • Usually the image itself is stored on the filesystem and the path to the image is stored in the db. This makes displaying them much easier because you have to set headers and such to output raw image data as an image: http://ellislab.com/forums/viewthread/179471/ Typical way: http://stackoverflow.com/q/9177399/183254 – stormdrain Apr 10 '13 at 13:00