【问题标题】:how upload file with save url in database如何在数据库中使用保存网址上传文件
【发布时间】:2016-05-09 08:45:38
【问题描述】:

我的应用程序视图中有这样的:

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

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

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

代码运行正常,但图片未上传,在数据库中插入只是上传路径,没有图片名称,不是完整路径 所以你能纠正我的错误或给我举个例子来上传带有其他字段的图片,但是每张图片都有观看文件夹,比如uploaded_news中的news,uploaded_post中的post

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

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

     $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' => $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);
    
    } else {
    
       // Some Error
    
    }
    

    使用回调的第二个想法

    文件名:Example.php

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

    查看表单打开

    <?php echo form_open_multipart('example/insertNews');?>
    
    // The rest of the form content goes here
    
    <?php echo form_close();?>
    

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

    【讨论】:

      【解决方案2】:

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

      更改您的代码

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

      到

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

      【讨论】:

      • 同样不上传不插入
      • 您确定您的enctype 已在您的表单中设置吗?如果是,你能提供一个 $_POST 的 var_dump() 吗?
      • 这是输出
         数组 (size=5) 'title' => string 'test title' (length=10) 'content' => string 'test content' (length=12) 'news_category_id' => 字符串'1'(长度=1)'img_url' => 字符串'./uploads/up_news/'(长度=18)'created_at' => 字符串'2016-05-09'(长度=10 )
      • 他仍然需要使用 $this->upload->data();在 $this->upload->do_upload('userfile'); 之后访问数据如用户指南中所述codeigniter.com/user_guide/libraries/…
      猜你喜欢
      • 2019-12-30
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 2019-04-30
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多