簡體   English   中英

圖像未上傳到數據庫codeigniter

[英]Image is not uploaded into database codeigniter

我正在嘗試在表格中上傳圖片。 表格中的所有數據都可以存儲到數據庫中接受圖像。 並且圖像也不會插入到upload_path中。 我想讓用戶填寫所有表單信息並上傳圖片,然后單擊“提交”按鈕,並將所有數據(包括圖片)存儲到數據庫中。 我的代碼有什么問題? 謝謝。

//view.php

<?php echo form_open_multipart('writereview/create');?>
<label for="sitename"><span>Sitename <span class="required">*</span></span><input type="text" class="input-field" name="sitename" value="" /></label>
<input type="file" name="images"  size="20" /> <br />
<label><span>&nbsp;</span><input type="submit" value="Submit" /></label>

//控制器

public function create() //insert data
{
    $data['title'] = 'Write Review'; // Capitalize the first letter 
    $this->load->helper(array('html', 'url', 'form'));

    $this->config_model->writereview();

    $this->load->view('templates/header', $data);
    $this->load->view('writereview/writereview');
    $this->load->view('templates/footer');
}

//模型

public function writereview()
{
    $this->load->helper('url');

    $type = explode('.', $_FILES["pic"]["name"]);
    $type = strtolower($type[count($type)-1]);
    $url = "./assets/images/".uniqid(rand()).'.'.$type;
    if(in_array($type, array("jpg", "jpeg", "gif", "png")))
        if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
            if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
                return $url;


    $data = array(
        'sitename' => $this->input->post('sitename'),
        'images'=>$this->input->post('images')
    );
    return $this->db->insert('review', $data);
}

更改您的插入數據數組

$data = array(
        'sitename' => $this->input->post('sitename'),
        'images'=>$uploaded_file_name
    );

還將$_FILES["pic"]["name"] to $_FILES["images"]["name"]

沒有輸入名稱為images的發布鍵,因為輸入類型是file ...

這是您示例的完整代碼...

        $image_name ='';
        if (isset($_FILES['images']['tmp_name']) && $_FILES['images']['tmp_name'] != '') 
        {
          $config['upload_path'] = 'assets/images/';
          $config['allowed_types'] = 'gif|jpg|png|jpeg';
          $config['max_size'] = '20000'; // change it to your max file size
          $this->load->library('upload', $config);
          $this->upload->initialize($config);
          if (!$this->upload->do_upload('images')) {//if upload error
            print_r($this->upload->display_errors());//print error
          }else{
            $data = $this->upload->data();
            $image_name = $data['file_name'];
          }
        }
        $data = array(
           'sitename' => $this->input->post('sitename'),
           'images'=>$image_name
        );
        return $this->db->insert('review', $data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM