CodeIgniter Forums

Full Version: Problem uploading files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ray73864[/eluser]
I am using CI 1.6.3 (no modifications) and each time i try to upload an image file the error being returned is: You did not select a file to upload.

I haved checked my html code and i indeed have a input file with the name 'userfile' and when i spit out the value of that field in the upload function it comes back with the filename (eg. blah.jpg) but with no pathnames or anything. I used the example in the user_guide.

The code in my controller:

Code:
function _image_upload($product_id)
    {
        $config['upload_path'] = '/files/images';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size']    = '500';
        $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());
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            $this->db->where('id',$product_id);
            $this->db->update('product',array('image'=>$data['full_path']));
        }
        
        return $error;
    }

The controller function for updating my database:

Code:
function update_master_product($productid, $action = "update")
    {
        if ($action == "update") {
            $data = array(
                'name'=>$this->input->post('product_name'),
                'product_code'=>$this->input->post('product_code'),
                'cat_id'=>$this->input->post('category'),
                'description'=>$this->input->post('rte')
            );
            
            $this->db->where('id',$productid);
            $this->db->update('product',$data);
            
            $errors = $this->_image_upload($productid);
            
            if ($this->db->affected_rows() > 0) {
                $this->message['type'] = "success";
                $this->message['text'] = 'Product updated successfully.';
            } else {
                $this->message['type'] = 'info';
                $this->message['text'] = 'Product not updated.';
                print_r($errors);
            }
        } else if ($action == "delete") {
            $this->db->where('id',$productid);
            $this->db->delete('product');
            
            if ($this->db->affected_rows() > 0) {
                $this->message['type'] = "success";
                $this->message['text'] = 'Product deleted successfully.';
            } else {
                $this->message['type'] = 'error';
                $this->message['text'] = 'Product could not be deleted.';
            }    
        } else {
            $this->message['type'] = 'error';
            $this->message['text'] = 'Invalid action performed on article while updating.';
        }
        
        $this->session->set_flashdata($this->message['type'], $this->message['text']);
        redirect('/admin_catalogue/products/', 'refresh');
    }

And the view itself:

Code:
<?php
    include_once "fckeditor/fckeditor.php";
?>
<!-- The main contents of the site -->
    <div id="content">
        <h3>Product: &lt;?=$product['name'] . " (" . $product['code'] . ")";?&gt;</h3>
        &lt;?=$this->session->flashdata('message');?&gt;
        <p>
            &lt;?=form_open_multipart('admin_catalogue/update_master_product/' . $product['id']);?&gt;
                <label for="product_name">Product Name: </label>
                &lt;input type="text" name="product_name" id="product_name" size="50" value="&lt;?=$product['name'];?&gt;" /&gt;
                <br />
                <label for="product_code">Product Code: </label>
                &lt;input type="text" name="product_code" id="product_code" size="50" maxlength="50" value="&lt;?=$product['code'];?&gt;" /&gt;
                <br />
                <label for="userfile">Product Image: </label>
                &lt;input type="file" name="userfile" id="userfile" size="100" accept="image/jpeg,image/gif,image/png" /&gt;
                <p>
                <label style="vertical-align: top" for="category">Category:</label>
                &lt;?php if ($category->num_rows() > 0): ?&gt;
                    <select name="category">
                        <option value="">None</option>
                        &lt;?php foreach ($category->result() as $row): ?&gt;
                            <option value="&lt;?=$row->id;?&gt;"
                                &lt;?=($product['category_id'] == $row->id) ? " selected=\"selected\"" : "";?&gt;
                            >&lt;?=$row->name;?&gt;</option>
                        &lt;?php endforeach; ?&gt;
                    </select>
                &lt;?php else: ?&gt;
                    No categories found.
                &lt;?php endif; ?&gt;
                </p>
                <br />
                <label for="rte">Product Description: </label>
                <p>
                &lt;?php
                    $oFCKeditor = new FCKeditor('rte');
                    $oFCKeditor->BasePath = '/fckeditor/';
                    $oFCKeditor->ToolbarSet = 'Normal';
                    $oFCKeditor->Config['ToolbarCanCollapse'] = false;
                    $oFCKeditor->Value = $product['description'];
                    $oFCKeditor->Create();
                ?&gt;
                </p>
                <p>
                    &lt;input type="submit" value="Update Product" /&gt;
                    &lt;input type="reset" /&gt;
                </p>
            &lt;/form&gt;
        </p>
    </div>

View code updated so that it shows 'form_open_multipart'

El Forum

[eluser]ray73864[/eluser]
just noticed that my form wasn't of type 'multipart' so have changed the 'form_open' to a 'form_open_multipart', but it still hasn't fixed the problem.

Actually, the error message has changed though, it now comes up with the error: The upload path does not appear to be valid.

I am using Opera 9.25, however the same error comes up under IE7 too.



Nevermind, i got it now, the upload_path is relative to the document root (or something like that), i changed '/files/images' to './files/images' and it works perfectly.