Welcome Guest, Not a member yet? Register   Sign In
Image upload
#11

[eluser]the_unforgiven[/eluser]
Ok and can you point me to how i shouls do this, can you show me code example for instance?
#12

[eluser]Wondering Coder[/eluser]
I think you don't need to use callback function coz file_upload has its own validation. As I've said in my latter post. You can achieve this by one function or separate function. I prefer separate function Big Grin. You just need to create the do_upload function and throw the success or error in the same variable and fetch that variable in your new_product.

Code:
function do_upload(){
        $this->load->library('upload', $config);
           if ( ! $this->upload->do_upload())
        {
            $image_data['error'] = $this->upload->display_errors('<p class="error">','</p>');
        }
        else
        {
        #$this->upload->do_upload();  
            $image_data = $this->upload->data();
        }
        return $image_data;
}

function newproduct()
{
$image = $this->do_upload()
if(! empty(!image['file_name']))
{
//success
}
else {
$data['error'] = $image['error'];
}
}
#13

[eluser]LuckyFella73[/eluser]
Quote:I think you don’t need to use callback function coz file_upload has its own validation.

The callback function would use the CI upload class and that way the CI validation.

If you don't set up a callback function you end up with multiple IF statements
in case the file is a "requiered" field. You have to compare if the "normal"
input field are valid, then if the file upload went fine and compare both results.
When using a callback function you have all validation in one place.

Could look like:
Code:
&lt;?php

function newproduct()
{      
        
    // Validation Rules
    $this->form_validation->set_rules('title', 'Title', 'trim');
    $this->form_validation->set_rules('description', 'Description', 'trim|required|max_length[120]');
    $this->form_validation->set_rules('image', 'Image', 'trim');
    $this->form_validation->set_rules('price', 'Price');

    $this->form_validation->set_rules('userfile', 'Your file', 'callback__do_upload_file'); // file would be requiered in this example

    $this->id = $this->uri->segment(3);
    $data['id'] = $this->id;

    // IF IS FALSE SHOW FORM
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('admin/newproduct');
    }
    //ELSE INSERT INTO DB
    else
    {    
        
                    
        $data = array(
            'title' => $this->input->post('title'),
            'description' => $this->input->post('description'),
            'price' => $this->input->post('price'),
            'status' => $this->input->post('status')
                                    
        );
        $this->db->insert('ci_products', $data);
        redirect('admin/products');    
        
    }
  
}

function _do_upload_file()
{
    $config['upload_path'] = 'YOUR_PATH';
    $config['allowed_types'] = 'ALLOWED_TYPES';
    $config['max_size'] = 'YOUR_MAX_SIZE';

    $this->load->library('upload', $config);
    if (!$this->upload->do_upload())
    {
        $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
        return FALSE;
    }
    else
    {
        $this->filedata = $this->upload->data(); // use $this->filedata for DB insert - like filename or whatever you need
        return TRUE;
    }
}

Don't forget to look at the user guide.
#14

[eluser]the_unforgiven[/eluser]
Cheers guys will see how i go on now, much appreciated Smile
#15

[eluser]the_unforgiven[/eluser]
Right works to a certain extent but when inserting into DB it's just showing a 0

Controller:
Code:
function newproduct()
    {      

        // Validation Rules
        $this->form_validation->set_rules('title', 'Title', 'trim');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|max_length[120]');
        $this->form_validation->set_rules('image', 'Image', 'trim');
        $this->form_validation->set_rules('price', 'Price');

        $this->form_validation->set_rules('userfile', 'Image', 'callback__do_upload_file'); // file would be requiered in this example

        $this->id = $this->uri->segment(3);
        $data['id'] = $this->id;

        // IF IS FALSE SHOW FORM
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/newproduct');
        }
        //ELSE INSERT INTO DB
        else
        {    

            
            $data = array(
                'title' => $this->input->post('title'),
                'description' => $this->input->post('description'),
            'image' => $this->input->post('userfile'),  // < IS THIS LINE RIGHT? WHAT SHOULD IT BE
                'price' => $this->input->post('price'),
                'status' => $this->input->post('status')

            );
            //$data = $this->upload->data('userfile');
            $this->db->insert('ci_products', $data);
            redirect('admin/products');    

        }

    }
#16

[eluser]LuckyFella73[/eluser]
Should be like this:
Code:
'image' => $this->filedata['file_name'],
#17

[eluser]the_unforgiven[/eluser]
OK again kinda worded but see image
#18

[eluser]LuckyFella73[/eluser]
In my callback for fileupload I have:
Code:
// upload ok:
$filedata = $this->upload->data();
$this->filename = $filedata['file_name'];

When inserting the name into db I use '$this->filename'
#19

[eluser]the_unforgiven[/eluser]
Mate you absolute legend that worked and now i have seen code i fully understand how this works, i have to see working example in order for it to sink in the brain lol an odd way of learning i know but still all the same it now works can't tell you how greatful i am?
#20

[eluser]LuckyFella73[/eluser]
Glad to hear it works for you now.
Btw - in case the file is not required you can do this in your controller
when setting up the rules:
Code:
if( isset($_FILES['userfile']['name']) AND strlen($_FILES['userfile']['name'])>0 )
{
    $this->form_validation->set_rules('userfile', 'Your File', 'callback__do_upload_file');
}

// $_FILES['userfile']... => 'userfile' has to match the name attribute of you input field

But then you have to declare $this->filename in your upload method or contructor
and set it to '' or whatever makes sense otherwise you get an error when trying
to insert the name into db.




Theme © iAndrew 2016 - Forum software by © MyBB