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()
    {
        $this->load->library('form_validation');

        $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);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->view('news/success');
        }
    }

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

Please help me..

Member Avatar for diafol

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.

Codeigniter with active record

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.