Codeigniter上传图片并插入我的文件夹

时间:2013-06-21 16:35:56

标签: php javascript html codeigniter

我正在使用codeigniter,我有一个表单,可以添加新项目。

在这个表单中,我有一个字段,我可以上传图像并插入我的数据库。

一切正常,可以插入我的数据库。

我的问题是,如何将我上传的图像添加到我的文件夹中?

路径: nameofproject/imagens/banner/

这是我的控制器:

function novo(){
    $this->load->helper(array('form', 'url')); 
    $this->load->library('form_validation');
    $this->form_validation->set_rules('banda', 'Banda', 'required');
    $this->form_validation->set_rules('data', 'Data', 'required');
    $this->form_validation->set_rules('hora_inicio', 'Hora In�cio', 'required|numeric');
    $this->form_validation->set_rules('hora_fim', 'Hora Fim', 'required|numeric');
    $this->form_validation->set_rules('morada', 'Morada', 'required'); 
    $this->form_validation->set_rules('preco', 'Pre�o', 'required|numeric'); 
    $this->form_validation->set_rules('aquisao_bilhetes', 'Aquisi��o de Bilhetes', 'required');
    $this->form_validation->set_rules('descricao', 'Observações', 'required');
    $this->form_validation->set_rules('image', 'Imagem', 'required|[image/jpeg|image/png]|file_path[../../imagens/banner/]');

    if ( ! $this->input->post())
    {
        $this->load->view("concertForm");
    }
    else
    {
        $dados=$this->input->post();
        $this->load->model("Dados_model");
        $results = $this->Dados_model->insere($dados);
    }
}

2 个答案:

答案 0 :(得分:0)

您是否希望在数据库中存储对图像的引用(即:您有一个文件名列,并且存储:myupload.jpg)或者您是否真的想将图像存储在数据库中?< / p>

如果是第一个,那么您需要使用$this->upload->data()帮助功能来获取有关文件的信息。

干杯

答案 1 :(得分:-1)

也许这可以帮助你解决问题,

1初始化上传课程

与CodeIgniter中的大多数其他类一样,使用 $ this-&gt; load-&gt; library 函数在控制器中初始化Upload类:

$this->load->library('upload');

2设置偏好设置

与其他库类似,您可以根据自己的偏好控制允许上传的内容。在上面构建的控制器中,您可以设置以下首选项:

$config['upload_path'] = './uploads/'; //you can change this set preferences for folder upload

$config['allowed_types'] = 'gif|jpg|png';

$config['max_size'] = '100';

$config['max_width'] = '1024';

$config['max_height'] = '768';

$this->load->library('upload', $config);

// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:

$this->upload->initialize($config);

你可以这个 User manual