【问题标题】:How to upload an image and save it to database in Codeigniter?如何上传图像并将其保存到 Codeigniter 中的数据库?
【发布时间】:2016-08-27 12:06:21
【问题描述】:

我知道这个问题已经被问了数百万次了,但是所有的问题对于初学者来说都有点太难了。

所以我正在寻找的是如何创建一个简单的按钮来上传图像并获取其完整路径并将其保存在表单的隐藏输入中,因此当我单击提交时,表单将插入带有其他输入,因此图像将被上传到目录。

这是一个简单的项目,一个基本的博客页面,我需要在其中保存以下输入: 1. 图片 2. 标题 3. 身体

我唯一无法完成的是图像部分。

查看代码如下:

<h1>Add New Post</h1>
    <br />


    <?=form_open('admin/add_post');?>

    <p>
      <input type="text" name="title"  />
    </p>
    <input hidden="hidden" name="date" value="<?php echo date('d-m-y');?>"/>



    <p>
      <textarea id="textarea" name="body" rows="10"></textarea>
    </p>



    <p>
      <input type="submit" value="Submit Post"  />
    </p>
    </form>

这是您点击提交时的控制器:

// Adds new posts to the database table
    function add_post()
    {
      $this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
      $this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');

      if ($this->form_validation->run() == TRUE) {
        // INSERTING GOES HERE..
        $this->db->insert('entries', $_POST);
        redirect('admin/posts');
      }
      else {

        redirect('admin/add');
      }

    }

所以我需要有人告诉我如何编写上传函数以将图像上传到同一页面和同一表单上,并将其路径保存到数据库中。

我可以让它保存路径,但我想先获取路径并将其添加到隐藏输入中的相同表单中,EX:

我真的需要你的帮助,如果你对如何帮助我有任何想法,请发表你的答案。

提前致谢。

【问题讨论】:

    标签: php html database codeigniter upload


    【解决方案1】:

    使用 CodeIgniter 实现这一点非常简单。

    我将使用版本 3.1.0 参考:

    查看:

    <?php echo form_open_multipart('upload/do_upload');?>
    
    <input type="file" name="userfile" size="20" />
    
    <br /><br />
    
    <input type="submit" value="upload" />
    
    </form>
    

    控制器:

     public function do_upload() {
    
    
            $fileData = array();
            // File upload script
            $this->upload->initialize(array(
                'upload_path' => './uploads/',
                'overwrite' => false,
                'max_filename' => 300,
                'encrypt_name' => true,
                'remove_spaces' => true,
                'allowed_types' => 'gif|jpg|png',
                'max_size' => 100,
                'xss_clean' => true,
            ));
    
            $this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
            $this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
    
    
    
            if ($this->form_validation->run() == TRUE) {
    
                if ($this->upload->do_upload('userfile')) {
    
                    $data = $this->upload->data(); // Get the file data
                    $fileData[] = $data; // It's an array with many data
                    // Interate throught the data to work with them
                    foreach ($fileData as $file) {
                        $file_data = $file;
                    }
    
                    var_dump($file_data);
    
                    $this->db->insert('entries', array(
                        // So you can work with the values, like:
                        'title' => $this->input->post('title', true), // TRUE is XSS protection
                        'body' => $this->input->post('body', true),
                        'file_name' => $file_data['file_name'],
                        'file_ext' => $file_data['file_ext'],
                    ));
    
    
    
                    $this->session->set_flashdata('success', 'Form submitted successfully');
                    redirect('admin/add');
                } else {
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                    redirect('admin/add');
                }
            } else {
                $this->session->set_flashdata('error', validation_errors());
                redirect('admin/add');
            }
        }
    

    您可以使用 var_dump 查看可用的数据,或查看文档(在底部)

    https://www.codeigniter.com/userguide3/libraries/file_uploading.html

    【讨论】:

    • 谢谢!!这工作得很好,但不完全是我要找的,我是codeigniter的新手,我想知道如何做到这一点:选择一个图像,当我点击提交>图像应该上传表单应该提交其他输入像文本输入,到数据库。我们在这里有什么可以做的吗?很抱歉打扰您。
    • 非常感谢,我做了一些更改并使用了另一种形式,并且能够将其与其他输入一起保存。
    • 我做了一些改变。你可以改进一下,看看。我很高兴为您提供帮助 =)
    猜你喜欢
    • 2021-09-16
    • 2015-04-15
    • 2011-10-25
    • 2017-12-20
    • 2017-06-28
    • 2015-03-02
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多