Welcome Guest, Not a member yet? Register   Sign In
Uploading Multiple Images
#1

[eluser]Kemik[/eluser]
Hello,

I'm trying to create a photo gallery where the user can add more upload fields to the form. For each upload field added, a description text box is added.

Does anyone have some sample code for multiple image uploads? I've never been very good with loops.
#2

[eluser]=G-Man=[/eluser]
totally writting this from memory, so it might need some refining

but if your form is set to multipart-formdata and your image fields are set as an array (name="image[]") then in PHP you should get away with

Code:
foreach ($_FILES['image'] as $image) {
    //do the single image upload stuff here
    if (is_uploaded_file($image['tmp_name'])) {
         //do you file moving stuff here
    } else {
       echo "Possible file upload attack: ";
       echo "filename '". $image['tmp_name'] . "'.";
    }
}

---------EDIT---------
just found this after posting
http://us3.php.net/manual/en/features.file-upload.php

look at example #3 its exactly what you want
#3

[eluser]Kemik[/eluser]
Eeek this is working out harder than expected.

I can get the files to upload fine. However, handling errors and validation is more difficult. I'm trying to get it all in to one controller and view if possible. Especially in he view.

At the moment I have

Code:
function upload()
    {
        $this->load->helper('form');
        
        $this->load->view('includes/header');
        $this->load->view('gallery_upload', array('errors' => ''));
        $this->load->view('includes/footer');
    }
    
    function upload_process()
    {    
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';

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

        foreach($_FILES as $key => $value)
        {
            if (!empty($key['name'])) {
                $this->upload->initialize($config);

                if (!$this->upload->do_upload($key)) {
                    $errors[] = $this->upload->display_errors();
                    $this->load->view('gallery_upload', $errors);
                } else {
                    //$this->gallery_model->process_pic();
                    
                }
            }
        }
        $this->session->set_flashdata('green', 'The photos have been uploaded successfully.');
        redirect('gallery/upload');
    }

The problem is, I need to try and add some validation to the image upload fields. There is not set number at the moment but I could limit it to five if required. If I leave an upload field blank it brings back an error.

Has anyone made a multiple image upload form and have their controller and view to hand to give me some help please? Handling errors is a pain in the back side for this. I hate loops :p




Theme © iAndrew 2016 - Forum software by © MyBB