我的应用程序视图中有:
<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中的新闻,uploaded_post 中的帖子
当你需要上传图像时,你需要使用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 中设置了基本Url
您的代码中有一个简单的错误,您错过了定义上传的文件
从更改代码
$upload_data = $this->upload->data();
至
$upload_data = $this->upload->do_upload('userfile'); //added do_upload('userfile') your input file name