我有一个很大的表单,我刚刚添加了一个文件输入,允许用户上传图像。
下面是新的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已正确提交,但文件已丢失。
我应该做一些不同的事情吗?不太确定哪里出了问题。
发布于 2011-01-14 06:17:22
首先,不要想当然地得到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/';发布于 2011-01-14 04:00:08
codeigniter希望您的文件输入名为userfile,除非您在代码中像这样指定,
$field_name = "some_field_name";
$this->upload->do_upload($field_name)发布于 2011-01-14 18:37:52
我不确定问题出在哪里。但下面的链接可能会对你有所帮助。
Upload file bug in codeigniter
您还可以初始化配置以设置首选项。
$this->upload->initialize($config);https://stackoverflow.com/questions/4684612
复制相似问题