Hi there i want to create a news item that posts to my database (this works fine) but i want to be able to upload an image in the same form and submit the file path to the news table where all the other content is placed.

Here is my View

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>
<?php echo form_open_multipart('news/create') ?>

<label for="title">Title</label>
<input type="input" name="title" /><br />

<label for="image">Featured Image</label>
<input type="file" name="userfile" size="20" /><br />

<label for="text">Text</label>
<textarea name="text"></textarea><br />

<input type="submit" name="submit" value="Create news item" />

<?php echo form_close(); ?>

And my model:

public function set_news()
        $this->load->view('news/create', array('error' => ' ' ));

        $slug = url_title($this->input->post('title'), 'dash', TRUE);
        $data = array(
            'post_title' => $this->input->post('title'),
            'slug' => $slug,
            'post_text' => $this->input->post('text')

        return $this->db->insert('news', $data);

my controller:

    public function create()

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'text', 'required');

        if ($this->form_validation->run() === FALSE)
            $this->load->view('templates/header', $data);


how would i go about uploading the image when i create the new article?

Please help me..

Member Avatar

Please state which framework you're using. Also you have no file handling code from what I can see. Have a look at file handling scripts (uploads). There are literally hundreds on Daniweb and thousands out in the wild.