代码:图片上传 [英] Codeigniter: Image Upload

查看:135
本文介绍了代码:图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型表单,我只是添加了一个文件输入以允许用户上传图片。

I have a large form, and I just added an file input to allow users to upload an image.

这是新的HTML:

<input type="file" name="file_1" />
<input type="text" name="image_description_1" class="text-input"/>

以下是新的_submit函数:

Here's the new _submit function:

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正确提交,

The description and user_id are correctly getting submitted, but the file is lost.

我应该做不同的事吗?

Should I be doing something different? Not really sure what's going wrong.

推荐答案

首先,不要得到 do_upload / code>为必要,您必须验证上传

if ( ! $this->upload->do_upload('file_1'))
{
    // something went wrong, display errors
}   
else
{
    // everything is fine
}

此外,很可能是您的 upload_path ,请检查我链接到的文档:

Also, most likely it's your upload_path, check the document I linked to:

//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/';

因此,您可能需要将设置更改为:

So you may need to change your setting to something like:

$config['upload_path'] = './assets/uploads/avatars/';

这篇关于代码:图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆