• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uploading Multiple Images


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.

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

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'] . "'.";

just found this after posting

look at example #3 its exactly what you want

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

function upload()
        $this->load->view('gallery_upload', array('errors' => ''));
    function upload_process()
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';


        foreach($_FILES as $key => $value)
            if (!empty($key['name'])) {

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

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.