我有一个大表单,我只是添加了一个文件输入,以允许用户上传图像。
以下是新的HTML:
<input type="file" name="file_1" />
<input type="text" name="image_description_1" class="text-input"/>
这是新的_submit函数:
if($this->CI->input->post('file_1')){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
$config['upload_path'] = realpath(APPPATH . '../assets/uploads/avatars');
$this->CI->load->library('upload', $config);
$this->CI->upload->do_upload();
$image_data = $this->CI->upload->data();
$image['description'] = $this->CI->input->post('image_description_1');
$image['user_id'] = $id;
$image['image'] = $image_data['file_name'];
$this->CI->db->insert('report_images',$image);
}
说明和user_id已正确提交,但文件已丢失。
我应该做些不同的事吗?不确定出了什么问题。
答案 0 :(得分:8)
首先,不要将do_upload()
视为理所当然,您必须验证upload:
if ( ! $this->upload->do_upload('file_1'))
{
// something went wrong, display errors
}
else
{
// everything is fine
}
此外,很有可能是你的upload_path
,请查看我链接到的文件:
//You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.
$config['upload_path'] = './uploads/';
因此,您可能需要将设置更改为:
$config['upload_path'] = './assets/uploads/avatars/';
答案 1 :(得分:4)
codeigniter期望您的文件输入被命名为userfile,除非您在代码中指定
$field_name = "some_field_name";
$this->upload->do_upload($field_name)
答案 2 :(得分:1)
我不确定问题是什么。但可能是以下链接可以帮助你。
Upload file bug in codeigniter
您也可以初始化配置以设置首选项。
$this->upload->initialize($config);
答案 3 :(得分:0)
确保输入文件的名称设置为&#34; userfile&#34;因为上传功能需要来自输入字段的文件,其名称设置为&#34; userfile&#34;