0

How would I upload an array of files with CodeIgniter? I have a form where I want users to be able to upload more than one file at a time. I have a javascript code that adds an extra file input field named "files[]" when they click on a submit button. This is the code I have so far, but it always says that I didn't select a file to upload in the display_errors function.

    <code>
foreach($this->input->post('files') as $i => $file){
                    $config = array(  //Preferences to validate the image before uploading it
                        'upload_path' => './attachments',
                        'allowed_types' => 'jpg|gif|bmp',
                        'max_size' => '100',
                        'max_width' => '950',
                        'max_height' => '631',
                        'overwrite' => FALSE,
                        'encrypt_name' => TRUE,
                        'remove_spaces' => TRUE
                        );
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload('files['.$i.']')){
                        echo $this->upload->file_data();
                    } else {
                        echo $this->upload->display_errors('<li>', '</li>');
                    }
            }
</code>

2 Answers 2

2

You can do this way.

In your view :

<?php echo form_label('Image: ','userfile1');?>
<?php echo form_upload('userfile1');?>
<?php echo form_label('Image: ','userfile2');?>
<?php echo form_upload('userfile2');?>
<!-- and so on --> 

Then in your controller, something like these…

//Set File Settings 
$config['upload_path'] = 'uploads/'; 
$config['allowed_types'] = 'jpg|png'; 
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 

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

// Validate files for upload section...
$success = array();
$errors = array();
$updload_files = array(
        'userfile1' => $_FILES['userfile1'],
        'userfile2' => $_FILES['userfile2'],
        // You can add more
        );

foreach($updload_files as $field => $updload_file)
{
    // Only process submitted file
    if($updload_file['error'] == 0)
    {
       // If there is an error, save it to error var
       if( ! $this->upload->do_upload($field) )
       {
           $errors[] = 'Failed to upload '.$field;  
       }

       // Use this to get the new file info if you need to insert it in a database
       $success[] = array( 'Success to upload '.$field => $this->upload->data());
    } 
    else 
    {
       $errors[] = $field . ' contain no data';  
    }
}
// Heres you could gettin know whats happen
var_dump($errors);
var_dump($success); 
Sign up to request clarification or add additional context in comments.

2 Comments

So I guess this is the only way to do it. How would I know how many inputs are going to be processed?
You can specify how much input, with sequenced number. Its like userfile1, userfile2, userfile3...userfile100, both in your view and in your controller.
0

Please use

                if( $this->upload->do_upload($files) ) {

instead of

                if($this->upload->do_upload('files['.$i.']')) {

If you are using second line you are uploading files with files[0] , files[1], ... names

As you are already have value for current loop stored in $file variable, you can directly use that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.