views:

78

answers:

1

Hi,

I am working on my second CI application. I have created some simple CRUD methods in my controller. My client has requested that each record includes an image. I have searched the forums and other resources for help but haven’t had much luck.

I have managed to upload files into a directory using the File Uploading Class, my problem though is how to associate the uploaded file/s with the relevant record.

Here are the relevant parts of my MVC.., any help/point in the right direction would be appreciated.

View - admin/locationEdit.php

<form method="post" action="<?php echo $action; ?>">
        <div class="data">
        <table>
            <tr>
                <td width="30%">ID</td>
                <td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->id; ?>"/></td>
                <input type="hidden" name="id" value="<?php echo $this->validation->id; ?>"/>
            </tr>
            <tr>
                <td valign="top">Name<span style="color:red;">*</span></td>
                <td><input type="text" name="name" class="text" value="<?php echo $this->validation->name; ?>"/>
                <?php echo $this->validation->name_error; ?></td>
            </tr>
            <tr>
                <td valign="top">Address<span style="color:red;">*</span></td>
                <td><input type="text" name="address" class="text" value="<?php echo $this->validation->address; ?>"/>
                    <?php echo $this->validation->address_error; ?></td>
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Save"/></td>
            </tr>
        </table>
        </div>
        </form> 

Controller - location.php

function add(){
        // set validation properties
        $this->_set_fields();

        // set common properties
        $data['title'] = 'Add new location';
        $data['message'] = '';
        $data['action'] = site_url('admin/location/addLocation');
        $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));



            // Write to $title
      $this->template->write('title', 'Admin - Add New Location!');
    // Write to $sidebar
      $this->template->write_view('content', 'admin/locationEdit', $data);
      // Render the template
      $this->template->render();

    }

function addLocation(){
        // set common properties
        $data['title'] = 'Add new location';
        $data['action'] = site_url('admin/location/addLocation');
        $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));

        // set validation properties
        $this->_set_fields();
        $this->_set_rules();

        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

         $path_to_uploads='./uploads';
          $config['upload_path'] = $path_to_uploads;
          $this->load->library('upload', $config);

            $upload_data=$this->upload->data();
            $file_name=$upload_data['file_name'];
            $full_file_path = $path_to_uploads.'/'.$file_name;

        // run validation
        if ($this->validation->run() == FALSE){
            $data['message'] = '';
        }else{


           // save data
            $location = array('name' => $this->input->post('name'),
                            'address' => $this->input->post('address'),
                            'image_url' => $full_file_path);
            $id = $this->locationModel->save($location);

            // set form input name="id"
            $this->validation->id = $id;

            // set user message
            $data['message'] = '<div class="success">add new location success</div>';
        }

        // Write to $title
      $this->template->write('title', 'Admin - Add New Location!');
    // Write to $sidebar
      $this->template->write_view('content', 'admin/locationEdit', $data);
      // Render the template
      $this->template->render();
    }
+2  A: 

Within your upload function, get the path of the uploaded file:

    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $path_to_uploads='./uploads';
    $config['upload_path'] = $path_to_uploads;
    $this->load->library('upload', $config);
    //add this
    $this->upload->initialize($config);
    if (!$this->upload->do_upload()){
        $error = $this->upload->display_errors();
        echo "<script>alert($error);</script>";
    }else{
        $upload_data=$this->upload->data();
        $file_name=$upload_data['file_name'];
        $full_file_path = $path_to_uploads.'/'.$file_name;
    }

then you can return $full_file_path back to the method that called it and insert it into the db, or just insert directly.

stormdrain
Thanks for the quick reply stormdrain, however, I am stuck at the last hurdle. When I insert the record the contents of the fields simply read ./uploads/ and when i check the uploads folder there is nothing there?What does this suggest?Thanks for your helpDan
Dan C
It suggests that the upload is failing for some reason. Can you edit your question above to include the code you're using to process the upload? When using the File Upload Class, if a failure occurs for some reason, it will usually tell you why. Have you set the permissions on the upload folder correctly (e.g. chmod 777)?
stormdrain
It is a local install on a mac, I have changed the properties to read/write for all on the uploads folder.I have added my new code above, can you see what I am doing wrong?Thanks, Dan
Dan C
It looks like you forgot to call the `$this->upload->do_upload()` method of the File Upload Class which actually does the uploading. I edited my answer above to reflect the changes. It is set up so that if there is an error with the upload, the error message will be sent to a javascript alert, otherwise it will set the `$full_file_path` variable.
stormdrain
Thankyou for your efforts it worked, you saved my life!
Dan C