Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter file upload not working

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.

like image 976
Dinuka Thilanga Avatar asked Mar 27 '13 10:03

Dinuka Thilanga


People also ask

How to set upload file path in CodeIgniter?

Simply create a new file called the upload. php, add the $config array in that file. Then save the file in: config/upload. php and it will be used automatically.

How to upload pdf file in CodeIgniter?

For Uploading files, as usually we need a Simple HTML form, with an input field and submit button. Here is the Code: 'file','name' => 'userfile')); echo form_submit('submit','upload'); echo form_close(); ?>

How to set max file upload size in CodeIgniter?

if you want to set bigger upload size to your system, you need to go to php. ini file and change this. upload_max_filesize = 2M; //change this to your desired size.


1 Answers

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.

like image 173
Svetoslav Avatar answered Sep 29 '22 14:09

Svetoslav