php - 如何在Codeigniter中上传图像并将其保存到数据库?

标签 php html database codeigniter upload

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

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

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

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

这是查看代码:

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

这是单击“提交”时的 Controller :

// 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');
      }

    }

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

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

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

提前致谢。

最佳答案

使用 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>

Controller :

 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

关于php - 如何在Codeigniter中上传图像并将其保存到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39180957/

相关文章:

python - 为什么 cx_oracle execute() 现在不喜欢我的字符串?

database - 以编程方式将文档添加到 Hummingbird/OpenText eDocs 数据库

php - 在 suitecrm 创建提醒工作流程的正确方法?

javascript - 使用 JS/PHP 验证登录表单

html - 为什么这张幻灯片中的第三张图片几乎被跳过?

javascript - 谷歌地图 v3 api map 没有完全加载

r - 合并 R 合并数据中的两行并删除重复项

java - Java 中的加密和 PHP 中的解密

javascript - 一页滚动 : Trying to redirect to home page and scroll to the div - Not Working?

javascript - 0 高度 div 的类在 textarea 上应用