我正在开发一个几乎已经完成的项目,但我添加了一些代码来上传图片,上传工作正常,但是无法正常工作的是将文件路径发送到数据库,我该怎么办呢?
这是控制器:
<?php
class Doctor extends Admin_Controller
{
public function __construct ()
{
parent::__construct();
$this->load->model('doctor_m');
}
public function index ()
{
// Fetch all doctors
$this->data['doctors'] = $this->doctor_m->get();
// Load view
$this->data['subview'] = 'admin/doctor/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function edit ($id = NULL)
{
// Fetch a doctor or set a new one
if ($id) {
$this->data['doctor'] = $this->doctor_m->get($id);
count($this->data['doctor']) || $this->data['errors'][] = 'doctor could not be found';
}
else {
$this->data['doctor'] = $this->doctor_m->get_new();
}
// Set up the form
$rules = $this->doctor_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$temp = $this->doctor_m->do_upload();
//$data['userfile'] = "'".$temp."'";
//$data['userfile'] = $this->doctor_m->array_from_post(array('userfile'));
//dump($data);
$data = $this->doctor_m->array_from_post(array('fullname', 'email', 'specialization', 'qualifications', 'publications', 'userfile'));
$this->doctor_m->save($data, $id);
redirect('admin/doctor');
}
// Load the view
$this->data['subview'] = 'admin/doctor/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function delete ($id)
{
$this->doctor_m->delete($id);
redirect('admin/doctor');
}
}
这是模特:
<?php
class Doctor_m extends MY_Model {
var $img;
protected $_gallery_path;
protected $_table_name = 'doctors';
protected $_order_by = 'fullname desc, id desc';
public $rules = array(
'fullname' => array(
'field' => 'fullname',
'label' => 'Full Name',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
'specialization' => array(
'field' => 'specialization',
'label' => 'Specialization',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
'publications' => array(
'field' => 'publications',
'label' => 'Publications',
'rules' => 'trim|max_length[100]|xss_clean'
),
'qualifications' => array(
'field' => 'qualifications',
'label' => 'Qualifications',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
'userfile' => array(
'field' => 'userfile',
'label' => 'Userfile',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
);
function __construct() {
parent::__construct();
$this->_gallery_path = realpath(APPPATH . '../public_html/img/doctor_image');
}
public function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png|JPG',
'upload_path' => $this->_gallery_path,
'max_size' => 2000,
'remove_spaces' => true,
'overwrite' => true,
);
$this->load->library('upload', $config);
if (! $this->upload->do_upload()) {
$this->data['error'] = array('error' => $this->upload->display_error());
} else {
$image_data = $this->upload->data();
$this->img = $image_data['full_path'];
}
$config = array(
'source_image' => $this->img,
'new_image' => $this->_gallery_path . '/thumbs',
'create_thumb' => true,
'maintain_ratio' => true,
'width' => 75,
'height' => 50,
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config = array(
'source_image' => $this->img,
'maintain_ratio' => false,
'width' => 400,
'height' => 300,
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
return $image = $this->img;
}
public function get_new() {
$doctor = new stdClass();
$doctor->fullname = '';
$doctor->specialization = '';
$doctor->publications = '';
$doctor->qualifications ='';
$doctor->email ='';
$doctor->userfile = '';
return $doctor;
}
public function save_image() {
}
}
这是视图:
<h3><?php echo empty($doctor->id) ? 'Add a new doctor' : 'Edit doctor'. " " . $doctor->fullname; ?></h3>
<?php echo validation_errors();?>
<?php echo form_open_multipart(); ?>
<?php// echo form_close(); ?>
<?php// echo form_open();?>
<table class="table">
<tr>
<td>Doctor's Image</td>
<td>
<?php
echo form_upload('userfile');
?>
</td>
</tr>
<tr>
<td>Full Name</td>
<td><?php echo form_input('fullname', set_value('fullname', $doctor->fullname)); ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo form_input('email', set_value('email', $doctor->email)); ?></td>
</tr>
<tr>
<td>Specialization</td>
<td><?php echo form_input('specialization', set_value('specialization', $doctor->specialization)); ?></td>
</tr>
<tr>
<td>Qualifications</td>
<td><?php echo form_input('qualifications', set_value('qualifications', $doctor->qualifications)); ?></td>
</tr>
<tr>
<td>Publications</td>
<td><?php echo form_input('publications', set_value('publications', $doctor->publications)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('upload', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>