如何在数据库中上传带有保存网址的文件

时间:2016-05-09 08:45:39

标签: php codeigniter file-upload

我在我的应用程序视图中有:



<div class="form-group">
  <label class="col-md-4 control-label" for="content_textarea">Content</label>
  <div class="col-md-4">
    <textarea class="form-control" id="content_textarea" name="ad_news_content"></textarea>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="news_category">Category</label>
  <div class="col-md-4">
    <select id="news_category" name="ad_news_category"  class="form-control">
      <?php
      foreach ($news_category_data as $ncat ) {?>
        <option
           value="<?php echo $ncat->nc_id ?>"><?php echo $ncat->nc_id ," - ", $ncat->news_category_name ?>
         </option>
      <?php } ?>
    </select>
  </div>
</div>
<div class="form-group">
  <label class="col-md-4 control-label" for="news_create_date">Date</label>
  <div class=" col-md-5">
                  <input  type="text" placeholder="click to show datepicker"  id="datepic">
              </div>
</div>
<!-- main image upload -->
</div>
<div class="form-group">
  <label class="col-md-4 control-label" for="news_upload_img">Main Image</label>
  <div class=" col-md-5">
<input type="file" name="userfile" size="20" />

              </div>
</div>
<!-- end of main image upload -->


<!-- Button (Double) -->

<div class="form-group">
  <label class="col-md-4 control-label" for="edit_btn"></label>
  <div class="col-md-8">
    <button  id="edit_btn" name="add_btn" class="btn btn-primary"
    formaction="<?php echo base_url() ."admin/news/insertNews"?>">Update</button>
&#13;
&#13;
&#13;

在我的控制器中,我有插入数据库和上传图像的功能

    public function insertNews()
{
            $config = array(
                'upload_path'   => './uploads/up_news',
                'allowed_types' => 'gif|jpg|png',
                'max_size'      => '100',
                'max_width'     => '1024',
                'max_height'    => '768',
                'encrypt_name'  => true,
            );

            $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                $upload_data = $this->upload->data();
                $data_ary = array(
                    'title'     => $upload_data['client_name'],
                    'file'      => $upload_data['file_name'],
                    'width'     => $upload_data['image_width'],
                    'height'    => $upload_data['image_height'],
                    'type'      => $upload_data['image_type'],
                    'size'      => $upload_data['file_size'],
                                    'path'          => $upload_data['full_path'],
                    'date'      => time(),
                );
                $data = array('upload_data' => $upload_data);
            $this->load->model('newsModel');
    $ad_ne_data = array(
    'titel' => $this->input->post('ad_news_title') ,
    'content' => $this->input->post('ad_news_content') ,
    'news_category_id' => $this->input->post('ad_news_category') ,
    'img_url' => $data_ary['path']."".$data_ary['file'],
    'created_at' => date("Y-m-d")
);

    $this->newsModel->addNews($ad_ne_data);
}

代码运行正常,但图像未上传,数据库中只插入上传路径而没有图像名称而不是完整路径 所以你可以纠正我的错误或者举例说明上传图片与其他字段,但每张图片都有像upload_news中的新闻,upload_post中的帖子等文章文件夹

2 个答案:

答案 0 :(得分:0)

您错过定义要上传的文件的代码中有一个简单的错误

更改您的代码
 $upload_data = $this->upload->data();

$upload_data = $this->upload->do_upload('userfile'); //added do_upload('userfile') your input file name

答案 1 :(得分:0)

当您需要上传图片时,需要使用do_upload()我建议您使用form_helper form_open_multipart

class Example extends CI_Controller {

public function __construct() {

    parent::__construct();

    $this->load->library('form_validation');
    $this->load->helper('form');

}

public function index() {

    $data['title'] = 'Ad';

    $this->form_validation->set_rules('ad_news_title', 'Ad Title', 'required');
    $this->form_validation->set_rules('ad_news_content', 'Content', 'callback_insertNews');

    if ($this->form_validation->run() == FALSE) {

        $this->load->view('your_view', $data);

    } else {

        redirect('success_controller');
    }

}

public function insertNews() {
     $config = array(
                'upload_path'   => './uploads/up_news',
                'allowed_types' => 'gif|jpg|png',
                'max_size'      => '100',
                'max_width'     => '1024',
                'max_height'    => '768',
                'encrypt_name'  => true,
     );

    $this->load->library('upload', $config);

    if ($this->upload->do_upload('userfile')) {

    $upload_data = $this->upload->data();

    $data_ary = array(
    'title'     => $upload_data['client_name'],
    'file'      => $upload_data['file_name'],
    'width'     => $upload_data['image_width'],
    'height'    => $upload_data['image_height'],
    'type'      => $upload_data['image_type'],
    'size'      => $upload_data['file_size'],
    'path'          => $upload_data['full_path'],
    'date'      => time(),
    );

    $data = array('upload_data' => $data_ary);

    $this->load->model('newsModel');

    $ad_ne_data = array(
        'titel' => $this->input->post('ad_news_title') ,
        'content' => $this->input->post('ad_news_content') ,
        'news_category_id' => $this->input->post('ad_news_category') ,
        'img_url' => $data_ary['path']."".$data_ary['file'],
        'created_at' => date("Y-m-d")
    );

    $this->newsModel->addNews($ad_ne_data);

    } else {

       // Some Error

       $this->form_validation_set_message('insertNews', $this->upload->display_errors());

       return FALSE;

    }

}


}

使用回调的第二个想法

文件名:Example.php

<?php echo form_open_multipart('example/insertNews');?>

// The rest of the form content goes here

<?php echo form_close();?>

查看表单打开

{{1}}

确保您已在CI3中设置了基本网址