file is not uploading in codeigniter

2019-09-09 16:25发布

问题:

I know this question may have been asked million of times. I searched and followed the required steps for uploading a file.But when i try to upload a file,I get the error of not selecting a file to upload. My controller is:

function save(){
        $this->load->library('upload');
        $config = array();
        $config['upload_path'] = './assets/images/site_data/images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']      = '100';
        $config['file_name'] = time();
        $config['overwrite']     = FALSE;
        $this->upload->initialize($config);
        if($this->upload->do_upload()){
                $fInfo = $this->upload->data();
                echo '<pre>';
                print_r($fInfo);
                exit;
            }
        else 
            {
            echo  $this->upload->display_errors();
        }   
    }

Any my view is:

<form method="post" action="<?php echo base_url()?>books/save" enctype="multipart/form-data">   
    <input type="file" name="cover_photo" id="cover_photo">
    <input type="submit" name="save">
</form>  

Can anyone tell me what is wrong with my code.Please forgive me if I have made a mistake. Thanks.

回答1:

The problem is that the class expects the file to have a name of userfile as seen in the documentation.

Either change the name of the file input or add the name you want to use to the do_upload call.

An example of the latter:

$field_name = "cover_photo";
$this->upload->do_upload($field_name)


回答2:

Just follow CI File Upload Documentation:

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
{
    $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}


回答3:

try the following code and modify as per your requirement

function save()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}


标签: codeigniter