S Vinesh
S Vinesh

Reputation: 539

CodeIgniter: Image is not uploading.

gallery.php - View

 <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <title>CI Gallery</title>
    </head>
    <body>


        <div id="upload">
            <?php
            $this->load->helper("form");
            echo form_open_multipart('gallery/up');
            ?>
            <input type="file" name="file">
            <?php 
            echo form_submit('upload','Upload');
            echo form_close();
            ?>      
        </div>
    </body>
    </html>

gallery.php-controller

<?php
class Gallery extends CI_Controller {

    function index() 
    {   
        $this->load->view('gallery');

    }

    function up() 
    {
            $config['upload_path'] = './images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000000';
            $config['max_width']  = '10240';
            $config['max_height']  = '7680';
            $this->load->library('upload',$config);
            $this->upload->do_upload('file');
            $this->load->view('gallery');
    }


}

These are my coding files. Here the file is not being uploaded. Someone please help. I feel that the library upload fails to get loaded and hence it stop working. If that is the problem how to come over that.

Upvotes: 3

Views: 5219

Answers (4)

S Vinesh
S Vinesh

Reputation: 539

The problem was that the folder in which the image was to be uploaded had no permission to write, so I made that folder writable, and it works fine now.

Upvotes: 0

Raja Ram T
Raja Ram T

Reputation: 864

you can write the uploading image server side code

function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

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

            print_r($error); //debug it here 

        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    } 

Upvotes: 1

Jinesh Gandhi
Jinesh Gandhi

Reputation: 77

Your code seems good no error. Try below code to check if image upload or not may be it is path issue ? copy below code and put above

"$this->load->view('gallery');" 

this in your up function of controller.

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

 // uploading failed. $error will holds the errors.
}
else
{
  $data = array('upload_data' => $this->upload->data());

 // uploading successfull, now do your further actions
}

Upvotes: 0

SagarPPanchal
SagarPPanchal

Reputation: 10131

add form tag in your html

<form enctype="multipart/form-data" name="" action=""></form>

Upvotes: 1

Related Questions