没有上传图片我在使用Upload Helper CodeIgniter时遇到了什么问题。它让我发疯了。
查看
<?php echo form_open_multipart('http://work/npanel/users/save'); ?>
<div class="form-group">
<?php echo form_label('Name'); ?>
<?php
$data_name = array(
'name' => 'name',
'class' => 'form-control',
);
echo form_input($data_name);
?>
</div>
<div class="form-group">
<?php echo form_label('Lastname'); ?>
<?php
$data_lastname = array(
'name' => 'lastname',
'class' => 'form-control',
);
echo form_input($data_lastname);
?>
</div>
<div class="form-group">
<?php echo form_label('Picture'); ?>
<?php
$data_upload = array(
'type' => 'file',
'name' => 'image'
);
echo form_upload($data_upload);
?>
</div>
<?php
$data_hidden = array(
'id' => $id
);
echo form_hidden($data_hidden);
?>
<?php
$send = array(
'class' =>'btn-primary',
'type' =>'submit',
'value' =>'Save'
);
echo form_submit($send);
?>
<?php echo form_close(); ?>
控制器
public function save($name, $lastname, $id, $image)
{
$name = $this->input->post('name');
$lastname = $this->input->post('lastname');
$id = $this->input->post('id');
$image = $this->input->post('image');
$this->load->model('User_model');
$last_id = $this->User_model->save($name, $lastname, $id, $image);
$config['upload_path'] = '/assets/img/users/';
$config['allowed_types'] = 'jpg|png|gif';
$config['remove_spaces'] = TRUE;
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$this->upload->do_upload("image");
$t = $this->upload->data;
$this->load->model('Images_model');
$this->Images_model->setImage($t, $last_id);
redirect('http://work.com/npanel/users');
}
不保存图片资产/ img / users&#39;文件夹并不生成文件名。我做错了什么?
答案 0 :(得分:0)
在form_open_multipart('users / save')上查看时,您只需要控制器名称和功能
如果您想要http://www.codeigniter.com/user_guide/libraries/form_validation.html
,可以使用表单验证确保已在config.php中设置base_url
的config.php
$config['base_url'] = 'http://work/npanel/';
控制器功能
Filename Users.php仅首字母大写
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
$this->load->model('user_model');
$this->load->model('images_model');
}
public function save($name,$lastname,$id,$image) {
$name = $this->input->post('name');
$lastname = $this->input->post('lastname');
$id = $this->input->post('id');
$image = $this->input->post('image');
$last_id = $this->user_model->save($name, $lastname, $id, $image);
$config['upload_path'] = './assets/img/users/';
//$config['upload_path'] = FCPATH . '/assets/img/users/';
$config['allowed_types'] = 'jpg|png|gif';
$config['remove_spaces'] = TRUE;
$config['max_size'] = '2048';
$this->upload->initialize($config);
$this->upload->do_upload("image");
$t = $this->upload->data;
$this->images_model->setImage($t['file_name'], $last_id);
redirect('users/save');
}
}
在视图中,您的论坛打开时应该只有控制器名称和功能,您也可以将文件夹名称放在文件夹
中<?php echo form_open_multipart('users/save'); ?>
<?php echo form_open_multipart('if_in_folder_name/users/save'); ?>