Welcome Guest, Not a member yet? Register   Sign In
Multiple File Upload
#1

[eluser]ShoeLace1291[/eluser]
I have a form where a user can upload photos to their gallery. I want them to be able to upload more than one photo at a time. So in my upload form, I have a file input called "files[]" along with a javascript code that let's them add more file fields with the same name.

The PHP script that I have made goes through several steps. First, it goes through the process of the actual file upload. If the file upload is a success, it then creates an attachment file(as a database record) and then it creates the photo record in the database. When I return the upload data array, none of the array keys have any values. The display_errors function doesn't return any errors, either. Any ideas?

Code:
$this->load->library('form_validation');
    $this->form_validation->set_rules('files', 'Choose File', 'required');
    if($this->form_validation->run() == FALSE){
        $data = array(
                    'ALBUM_TITLE' => $album['ALBUM_TITLE'],
                    'FORM_OPEN' => form_open('gallery/photos/upload/'.$this->uri->segment(4)),
                    'VALIDATION_ERRORS' => validation_errors()
                    );
        $this->parser->parse('gallery/photo_upload.tpl', $data);
    } else {
        $limit = count($this->input->post('files'));
        $photo_ids = '';
        for($i=0;$i<=$limit;$i++){
            $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.']')){
                $file = $this->upload->data();
                $info = array(  //Information to pass to the attachment class for creation
                        'author_id' => $member['id'],
                        'file_name' => $file['file_name'],
                        'file_size' => $file['file_size'],
                        'file_width' => $file['file_width'],
                        'file_height' => $file['file_height']
                        );
                if($this->attachment->create($info)){
                    $info = array( //Information to pass to the photo class for creation(upload data)
                            'author_id' => $member['id'],
                            'date_posted' => now(),
                            'attachment_id' => $this->attachment->insert_id
                            );
                    if($this->photo->create($info)){
                        $photo_ids[] = $this->photo->insert_id;
                        $photo_errors = 0;
                    } else {
                        $photo_errors = $photo_errors + 1;
                    }
                } else {
                    $attachment_errors = $attachment_errors + 1;
                }
            } else {
                    $this->upload->display_errors('<p>', '</p>');
            }
        
        }
        
    
        
    }
#2

[eluser]toopay[/eluser]
You can do this way.

In your view :
Code:
&lt;?php echo form_label('Image: ','userfile1');?&gt;
&lt;?php echo form_upload('userfile1');?&gt;
&lt;?php echo form_label('Image: ','userfile2');?&gt;
&lt;?php echo form_upload('userfile2');?&gt;
&lt;!-- and so on --&gt;

Then in your controller, something like these...
Code:
//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);
#3

[eluser]ShoeLace1291[/eluser]
That's not how I'm doing it. The number of files being uploaded will be a random number chosen by the user and will be stored in an array called "file[]".
#4

[eluser]toopay[/eluser]
[quote author="ShoeLace1291" date="1311773195"]That's not how I'm doing it. The number of files being uploaded will be a random number chosen by the user and will be stored in an array called "file[]".[/quote]

What is the different? Above example just showing you the basic of how to handle multiple $_FILES. It still use some array pattern after all. You could just change userfile1, userfile2 with file[], indeed. Just iterate each field(s) with above mechanism/procedure.
#5

[eluser]ShoeLace1291[/eluser]
Because in order to iterate through the inputs I need to know how many inputs there are. That's why I'm using the array because i can just use count($this->input->post('file')) so I can use a for statement.
#6

[eluser]LuckyFella73[/eluser]
The problem with your script is that the files selected to
upload are not contained in the POST array!

Code:
// won't work with files!
$count = count($this->input->post(‘file’));

You have to count the $_FILES array and loop through that - like
toopay said.
#7

[eluser]Flemming[/eluser]
For a multiple image upload feature with style and progress bar I've used plupload (http://www.plupload.com) and plugged it in to codeigniters upload class seamlessly. You might want to try that if you have a bit of time to spare to integrate it.




Theme © iAndrew 2016 - Forum software by © MyBB