Upload multiple files with codeigniter

// From : http://darrenonthe.net/2011/05/08/upload-multiple-files-with-codeigniter/

// The View upload_form.php : 

<?php echo form_open_multipart('upload');  ?>
<p>
    <?php echo form_label('File 1', 'userfile') ?>
    <?php echo form_upload('userfile') ?>
</p>
<p>
    <?php echo form_label('File 2', 'userfile1') ?>
    <?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>

// The controller upload.php :

function index()
{
    // Has the form been posted?
    if (isset($_POST['submit']))
    {
        // Load the library - no config specified here
        $this->load->library('upload');
 
        // Check if there was a file uploaded - there are other ways to
        // check this such as checking the 'error' for the file - if error
        // is 0, you are good to code
        if (!empty($_FILES['userfile']['name']))
        {
            // Specify configuration for File 1
            $config['upload_path'] = 'uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';      
 
            // Initialize config for File 1
            $this->upload->initialize($config);
 
            // Upload file 1
            if ($this->upload->do_upload('userfile'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }
 
        }
 
        // Do we have a second file?
        if (!empty($_FILES['userfile1']['name']))
        {
            // Config for File 2 - can be completely different to file 1's config
            // or if you want to stick with config for file 1, do nothing!
            $config['upload_path'] = 'uploads/dir2/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
 
            // Initialize the new config
            $this->upload->initialize($config);
 
            // Upload the second file
            if ($this->upload->do_upload('userfile1'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }
 
        }
    }
    else
    {
        $this->load->view("upload_form");
    }
}
 
Processing your request, Please wait....

No Responses to “Upload multiple files with codeigniter”

Comments (Your Comments)

Leave a Reply

You must be logged in to post a comment.