Hi guys i want to know how i would store uploads(path's) in the database here is my Controller, View what should go into the Model?

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {

function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}

function index()
{
$this->load->view('admin/dashboard', array('error' => ' ' ));
}

function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$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());

$this->load->view('admin/dashboard', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());

$this->load->view('./upload_success', $data);
}
}
}

The View

<?php echo $error; ?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />
<?php echo form_close();

The Success Page

<h3>Your file was successfully uploaded!</h3>

<ul>
    <?php foreach ($upload_data as $item => $value):?>
        <li><?php echo $item;?>: <?php echo $value;?></li>
    <?php endforeach; ?>
</ul>

<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

I want to be able to upload the path to the database and then how would i display the image in a specific post??? how would i go about this?

Member Avatar for diafol

You should tag this or have Codeigniter in the title. MVC would be another good tag.

For me, controllers should be as thin as possible, with most of the internal workings of the object held in the model. If you start writing copious amounts of code in controllers you start duplicating code all over the place. Maintenance becomes an issue too.

Generic helper classes and functions may be useful too - and upload handler would be perfect for this if you are using uploads with more than one ORM - or extend the base model with an upload method.

However, CI is very loose wrt its MVC.

Are you using ORM? e.g. Doctrine or Datamapper?
Anyway, here's some background on ORM/Datamapper/ActiveRecord:

http://culttt.com/2014/06/18/whats-difference-active-record-data-mapper/

CI basics about Models:

https://ellislab.com/codeigniter/user-guide/general/models.html

Here's an interesting discussion - the accepted answer is along my own way of thinking:

http://stackoverflow.com/questions/3263452/use-of-upload-class-in-codeigniter-model-or-controller

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.