Codeigniter file upload not working

2019-02-23 10:00发布

问题:

I write code for upload a image file. But it always show You did not select a file to upload. This is my code.

view

<?php echo form_open_multipart('template/advertisement', array('id' => 'template-add-form', 'class' => 'form-horizontal')); ?>
    <?php echo form_fieldset('Add Advertisement', array('class' => '')); ?>
    <div class="control-group">
        <label class="control-label" for="add_l">Left Advertisement </label>
        <div class="controls">
            <input type="file" name="add_l"/>
        </div>
    </div>  
    <div class="control-group">
        <div class="controls">
            <input type="submit" name="add" value="Update Advertisement" class="btn btn-primary" /> 
        </div>
    </div>  
    <?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>

controller

public function advertisement()
    {
        $headerData = array();
        $headerData['title'] = ucfirst('Advertisement');

        $data = array();

        if ($this->input->post() !== FALSE) {   
            $config = array();
            $config['upload_path'] = './images/add';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['max_filename'] = '100';

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

            if ($this->upload->do_upload()) {
                $data['message'] = $this->upload->data();                       
            } else {
                $data['error'] = $this->upload->display_errors('<div class="alert alert-error">', '</div>');                
            }
        }

        $this->load->view('template/header.php', $headerData);
        $this->load->view('template/advertisement.php', $data);
        $this->load->view('template/footer.php');       
    }

Please help me.

回答1:

At your PHP change

$this->upload->do_upload()

to

$this->upload->do_upload("add_1")

where "add_1" is your input name attribute.

Codeigniter UPLOAD library docs

P.S. At your HTML the Label attribute for="" is targeting input ID so you have to add id to your input.



回答2:

i recenly got that problem and solve it. you can check is module fileinfo in php already on? if you can access to CPanel, you can go to SOFTWARE TAB then search Select PHP Version, then you can see list of module and check that module fileinfo. and try to upload again.