开发者

How to upload an array of files with CodeIgniter?

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>


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); 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜