繁体   English   中英

Codeigniter和Datamapper:将上传的图像保存到数据库不起作用

[英]Codeigniter & Datamapper: Saving uploaded image to database doesn't work

我正在尝试使用datamapper来帮助我并将file_name存储在数据库中。 首先,我使用Codeigniter手册中给出的do_upload的基本示例进行了此操作,最后我编写了3行datamapper来查看具有id和url的简单表。 我运行它并保存好。

现在的问题是,当我尝试在我的add_article方法中调用do_upload函数时,以便在使用图像上传文章时保存文章的所有详细信息以及上传的图像名称。 但是,它不起作用,我尝试了所有类型。 这是我所拥有的:

在我的控制器中:

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();


}

我的观点:

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

尝试这个。

相反,实例化一个新的Article_Modeldo_upload方法,只需从返回图像名称do_upload方法,并设置的url属性Article_Model其保存到数据库之前。

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'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM