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

[eluser]the_unforgiven[/eluser]
HI Guys,

What is the easiest way to upload an image from a form (see both form code and controller below)

In a controller to upload and check the image and then insert with all other data in the the database??

Form in view file:
Code:
<?php include ('header.php'); ?>
<h2>Add New Product</h2>
&lt;?php

// Show Errors
echo validation_errors('<div class="error">', '</div>');
// Get ID
$id = $this->uri->segment(3);

echo form_open_multipart('admin/newproduct');
echo '<table name="editform">
    <tr>
        <td class="table_title">Product Title:</td>
        <td>&lt;input type="text" name="name" value="" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title">Description:</td>
        <td>&lt;textarea name="description" cols="50" rows="8"&gt;&lt;/textarea></td>
    </tr>
    <tr>
        <td class="table_title">Image:</td>
        <td>&lt;input type="file" name="userfile" size="20" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title">Price: &pound;&nbsp;</td>
        <td>&lt;input type="text" name="price" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title">Status:</td>
        <td><select name="status">
        <option value="active">Active</option>
        <option value="inactive">Inactive</option>
        </select>
        </td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="hidden" name="lang_id" value="1" /&gt;&lt;/td>
        &lt;input type="hidden" name="form" id="form" value="edit" /&gt;
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="submit" name="submit" value="Save" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td><a href="/admin/products" title="Back">Back</a></td>
    </tr>
</table>';
echo form_close();
?&gt;

&lt;?php include ('footer.php'); ?&gt;

Controller ( action for the insert and upload)
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->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');    
            
        }
      
    }

I want that function in the controller to handle all data including uploading the image to a specific folder say admin/uploads for instance but not sure where it would go in the controller function to insert everything from the form.

Any idea's suggestions welcome and I'm still learning so please be patient with me please.

Thanx in advance
#2

[eluser]the_unforgiven[/eluser]
Anyone?
#3

[eluser]Wondering Coder[/eluser]
why don't you use file_upload in CI. That's what I used 1 week ago in app. You can read about this in the userguide. You can set your own upload path using the upload library.
#4

[eluser]the_unforgiven[/eluser]
that's what i want to use but it need to be in the same function rather then have two functions and I know how it works but not sure where to put it within my code already
#5

[eluser]the_unforgiven[/eluser]
OR does it recongise that it needs the do_upload function from the form??

advise please?
#6

[eluser]Wondering Coder[/eluser]
to make your code neat and readable you can separate you function for uploading then just call it to your other controller, just have the same variable when upload success and upload error and fetch the to your new_product function. It doesn't really need to be name do_upload you can name anything you want. Just make sure the function name you've given is the one you'll use in the form. Big Grin

Also since you said you want to fit it all in just one controller you could also achieve this. It will just be messy for me. Although any way is good.
#7

[eluser]the_unforgiven[/eluser]
Right think I understand but where in the newproduct function would i put the upload part?
#8

[eluser]Team Kulafihi[/eluser]
[quote author="the_unforgiven" date="1313696611"]Right think I understand but where in the newproduct function would i put the upload part?[/quote]

Here is a great tutorial on file upload and image manipulation Tutorial
#9

[eluser]the_unforgiven[/eluser]
[quote author="Team Kulafihi" date="1313698100"][quote author="the_unforgiven" date="1313696611"]Right think I understand but where in the newproduct function would i put the upload part?[/quote]

Here is a great tutorial on file upload and image manipulation Tutorial[/quote]

Totally agree but still not sure where to put the code, thats what I'm getting at
#10

[eluser]LuckyFella73[/eluser]
You can build a callback function for uploading the file
and then set a validation rule for the fileupload.




Theme © iAndrew 2016 - Forum software by © MyBB