当我想更新数据库表中有图像上传的记录时,它不能上传图像,但所有其他数据更新成功下面是更新函数:
$config['upload_path'] = './img/products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_header_view');
$this->load->view("admin/admin_menu_view");
$this->load->view('admin/admin_rewarding_view', $error);
$this->load->view('admin/admin_footer_view');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename = $data['upload_data']['file_name'];
$data= array(
'reward' => $this->input->post('reward'),
'amount_of_points' => $this->input->post('amount_of_points'),
'img' => $filename
);
$this->db->where('id',$this->input->post('id'));
$this->db->update('rewards',$data);
redirect("admin/rewarding_system");
}
if ( ! $this->upload->do_upload($this->input->post('upload_name_attr')) //or however you are getting this file to the server
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_header_view');
$this->load->view("admin/admin_menu_view");
$this->load->view('admin/admin_rewarding_view', $error);
$this->load->view('admin/admin_footer_view');
}
使用这个。我在以身作则。根据需求,进行必要的变更。这就是我上传文件的方法。这对你也有帮助。就这样过一遍。那里有很多垃圾代码,都忽略了。想用什么就用什么。希望。我帮了你。
<视图/h2><?php echo form_open_multipart('welcome/do_upload'); ?>
<input type='text' class='form-control' name='FileTitle'> <br>
<textarea class='form-control' name='FileDesc' rows='4'></textarea> <br>
<input type='file' class='form-control' name='ChangedFileName'>
</form>
h2控制器 >class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->library('session'); // Start Session
$this->load->helper('form');
$this->load->library('form_validation');
}
function do_upload()
{
$FileDescription = $this->input->post('FileDesc');
$FileID = $this->input->post('FileID');
$FileTitle = $this->input->post('FileTitle');
$UploadDirectory='assets/Upload/';
$FileName = $_FILES['ChangedFileName']['name'];
$EncFileName=time().$FileName;
if($FileName!='')
{
if(move_uploaded_file($_FILES['ChangedFileName']['tmp_name'], $UploadDirectory.$EncFileName))
{
$FileUpload=$this->news_model->UploadEditedFiles($EncFileName,$FileID,$FileTitle,$FileDescription);
if($FileUpload=='True')
{
redirect('welcome/editrecord/'.$FileID);
}
}
}
}
<?php echo form_open_multipart('welcome/do_upload'); ?>
<input type='text' class='form-control' name='FileTitle'> <br>
<textarea class='form-control' name='FileDesc' rows='4'></textarea> <br>
<input type='file' class='form-control' name='ChangedFileName'>
</form>
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->library('session'); // Start Session
$this->load->helper('form');
$this->load->library('form_validation');
}
function do_upload()
{
$FileDescription = $this->input->post('FileDesc');
$FileID = $this->input->post('FileID');
$FileTitle = $this->input->post('FileTitle');
$UploadDirectory='assets/Upload/';
$FileName = $_FILES['ChangedFileName']['name'];
$EncFileName=time().$FileName;
if($FileName!='')
{
if(move_uploaded_file($_FILES['ChangedFileName']['tmp_name'], $UploadDirectory.$EncFileName))
{
$FileUpload=$this->news_model->UploadEditedFiles($EncFileName,$FileID,$FileTitle,$FileDescription);
if($FileUpload=='True')
{
redirect('welcome/editrecord/'.$FileID);
}
}
}
}
}
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function UploadEditedFiles($EncFileName,$FileID,$FileTitle,$FileDescription)
{
$data = array('FilePath' => $EncFileName,'FileID'=>$FileID);
$this->db->where('FileID', $FileID);
$this->db->insert('MemberFiles', $data);
}
}
检查您的Initialization
:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->upload->initialize($config);
$this->load->library('upload', $config);