Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter V 2.0.2 Image Upload problem
#1

[eluser]Manjunath Reddy[/eluser]
Dear All,
I am facing problem with uploading image.
CodeIgniter Version = 2.0.2,
I load the Upload library and my upload form code does not even call the Upload method do_upload.
here is my source code.

Code:
<?php
class Uploadtest extends CI_Controller{
    var $upload_path;
  
                
    public function __construct(){
        parent::__construct();
    }
    
    function index(){
        $this->upload_path = realpath(APPPATH . '../upload');
              
        if($this->input->post('upload')){
            echo "Working.....";
            $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->upload_path ,
            'max_size' => 2000
        );
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            //[b]Here it does not even call the do_upload() method[/b]
            $image_data = $this->upload->data();
            print_r($config);
            echo "<br>";
            $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->upload_path.'/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 100
        );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            print_r($config);
            
        }
        $this->load->view('admin/uploadtest');
    }
}
?&gt;

My View
Code:
&lt;form name="form1" action="uploadtest" method="post" enctype="multipart/form-data"&gt;
       &lt;?php
      echo form_upload('userfile');
       ?&gt;
       &lt;input type="submit" name="upload" value="Upload"&gt;
       &lt;/form&gt;

Here is the debug code I have included in the system/Upload.php
Code:
public function do_upload($field = 'userfile')
    {
echo "inside do upload";
exit;
    // Is $_FILES[$field] set? If not, no reason to continue.
        if ( ! isset($_FILES[$field]))
        {
            $this->set_error('upload_no_file_selected');
            return FALSE;
        }
in the above case, if input file name is not "userfile", does it work?...

If Im not wrong, why they force to use "userfile" as name in &lt;input type='file' name='userfile'&gt;

what If I have to give different name?
:-) is there an example for CI V2.0.2, I could follow up.

Is there something wrong with CI 2.0.2, Uploading API
#2

[eluser]StefanAlexandru[/eluser]
Hi,
I think your problem is that you are not calling the method inside controller.
Try to use form helper and open your form like this:
form_open_multipart('upload/do_upload');?&gt;

Or change the action of your form like this: controller/function instead of "uploadtest"
#3

[eluser]Manjunath Reddy[/eluser]
Hi Stefan,
Thanks for your reply :-), I appreciate that.
First thing I'm calling the do_upload inside the controller itself :-).

Secondly,
Code:
&lt;form name="form1" action="uploadtest" method="post" enctype="multipart/form-data"&gt;

Here my form action directly pointing to Controller's('Uploadtest') index function, I guess I do not need to do controller/function right :-).
#4

[eluser]StefanAlexandru[/eluser]
Why don't you just try uploadtest/index ..
Anyway sorry for the first reply... I am on my phone right now.. So maybe that's why I didn't pay attention to your entire post.
#5

[eluser]LuckyFella73[/eluser]
Your controller should more look like this:

Code:
&lt;?php
class Uploadtest extends CI_Controller{
    var $upload_path;
  
                
    public function __construct(){
        parent::__construct();
    }
    
    function index()
    {
        $this->upload_path = realpath(APPPATH . '../upload');
        
        $config['upload_path'] = $this->upload_path;
        $config['allowed_types'] = 'jpg|jpeg|gif|png';
        $config['max_size']    = '2000';        
        $this->load->library('upload', $config);
        
        // if you want a different name as "userfile" in your form:
        # $field_name = "some_field_name";
        # if ( ! $this->upload->do_upload($fieldname))
        
        // if the input/file name is "userfile":
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            // display the form again
            $this->load->view('admin/uploadtest', $error);
        }
        else
        {
            $image_data = $this->upload->data();
            print_r($config);
            echo "<br>";
            
            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->upload_path.'/thumbs',
                'maintain_ration' => true,
                'width' => 150,
                'height' => 100
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            print_r($config);
            
            $data = array('upload_data' => $image_data);
            
            // display success page:
            $this->load->view('upload_success', $data); // maybe you want a success page
        }
    }
}
?&gt;

In your form-action I would better set this:
Code:
&lt;form name="form1" action="&lt;?php echo site_url();?&gt;/uploadtest" method="post" enctype="multipart/form-data"&gt;
#6

[eluser]Manjunath Reddy[/eluser]
Hi All,
Thank you very much for you time, Its really helpful.
after combining above logic's, manage to run it. :-). Really appreciate your time. Thumbs up.




Theme © iAndrew 2016 - Forum software by © MyBB