问题描述:

No image being upload what am I'm doing wrong with the Upload Helper CodeIgniter. It's driving me crazy.

View

<?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(); ?>

Controller

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

}

Does not save image 'assets / img / users' folder and does not generate the file name. What am I doing wrong?

网友答案:

On view on your form_open_multipart('users/save') you only need the controller name and function

You can use form validation if you want to http://www.codeigniter.com/user_guide/libraries/form_validation.html

Make sure you have set your base_url in config.php

config.php

$config['base_url'] = 'http://work/npanel/';

Controller function

Filename Users.php first letter upper case only

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

    }

}

On view your forum open should only controller name and function you can also put folder name if in folder

<?php echo form_open_multipart('users/save'); ?>

<?php echo form_open_multipart('if_in_folder_name/users/save'); ?>
相关阅读:
Top