Question

I am trying to use datamapper to help me out and store the file_name in the database. First I did it with the basic example of do_upload given in Codeigniter manual in the end I wrote 3 lines of datamapper to look at a simple table with id and url. I run it and it saves it well.

The problem now is when I am trying to call the do_upload function in my add_article method so when an article is uploaded with the image to save all the details of an article + the uploaded image name. However it doesn't work and I try all kind of types. Here is what I have:

In my Controller:

public function add_article($id = NULL)
{
    $articles = new Article_model();
    $article = $articles->where('id', $id)->get();

    $article->title = $this->input->post('title');
    $article->text = $this->input->post('text');


    // Try to upload the image
    if ($this->input->post('submit'))
    {
        $this->do_upload();
    }

    if ($article->save())
    {
        echo '<p>You have successfully added an article</p>';
        redirect('admin/article/');
    }
    else
    {
        echo '<p>' . $article->error->string . '</p>';
    }
}

   public function do_upload()
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        );

    $this->load->library('upload' , $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $article = new Article_model();
    $article->url = $image_data['file_name'];
    $article->save();


}

My view:

<?php echo form_open_multipart('admin/article/add_article'); ?>
<table class="table">
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
    </tr>
    <tr>
        <td>Image</td>
        <td><?php echo form_upload('userfile'); ?></td>
    </tr>
    <tr>
        <td>Body</td>
        <td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>       
</table>
Was it helpful?

Solution

Try this.

Instead of instantiating a new Article_Model in the do_upload method, simply return the image name from the do_upload method, and set the url property of the Article_Model before saving it to the database.

public function add_article($id = NULL)
{
    $articles = new Article_model();
    $article = $articles->where('id', $id)->get();

    $article->title = $this->input->post('title');
    $article->text = $this->input->post('text');


    // Try to upload the image
    if ($this->input->post('submit'))
    {
        $article->url = $this->do_upload();
    }

    if ($article->save())
    {
        echo '<p>You have successfully added an article</p>';
        redirect('admin/article/');
    }
    else
    {
        echo '<p>' . $article->error->string . '</p>';
    }
}

public function do_upload()
{
    $config = array(
    'allowed_types' => 'jpg|jpeg|gif|png',
    'upload_path' => $this->gallery_path,
    );

    $this->load->library('upload' , $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    return $image_data['file_name'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top