0

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..

2
Contributors
2
Replies
12
Views
2 Years
Discussion Span
Last Post by Ventech_IT
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.