Jess McKenzie
Jess McKenzie

Reputation: 8385

Codeigniter -> Image Uploading Part 2 -> Multiple Images

I am trying to create a multiple image uploader and I have come across this link. What I am confused about in relation to my code below and the link is that do I have to have 2

$this->upload->do_upload(); functions to run my code or how do I use

$this->upload->initialize($config); in the below situation?

Code:

//Image Upload Function

$conceptOne = 'conceptOne';
$conceptTwo = 'conceptTwo';

$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);

if(!is_dir($location.$folderName))
{   
    mkdir($location.$folderName);
    chmod($location.$folderName, 0777);

    //Set File Settings 
    $config['upload_path'] = $location.$folderName; 
    $config['allowed_types'] = 'jpg|png|pdf'; 
    $config['file_name'] = $conceptOne;
    $config['remove_spaces'] = TRUE;
    $config['overwrite'] = TRUE;
    $config['max_size'] = '1024'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 

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

    print_r($config);

    if(!$this->upload->do_upload($conceptOne)) { #= try upload
        $data['uploadError'] = array('uploadError' => $this->upload->display_errors()); #Error
        $this->load->view('layout', $data);
    } // Do upload
    else{
        $data = array('upload_data' => $this->upload->data($conceptOne));
    }// end else
}// end if folder

Upvotes: 0

Views: 1469

Answers (2)

Mischa
Mischa

Reputation: 43298

Kemal is right: you have to iterate over the files you have. I would put the "concepts" in an array, so you can use foreach:

// Load upload library without any configuration
$this->load->library('upload');

$concepts = array('conceptOne','conceptTwo');

$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);

if(!is_dir($location.$folderName))
{   
    mkdir($location.$folderName);
    chmod($location.$folderName, 0777);
}

$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';

// Upload 'concepts'
foreach($concepts as $concept)
{
    $config['file_name'] = $concept;
    $this->upload->initialize($config);
    $this->upload->do_upload($concept);
}

// Upload logo
$config['file_name'] = 'logo-filename.gif';
$this->upload->initialize($config);
$this->upload->do_upload('logo');

Upvotes: 1

Fad
Fad

Reputation: 9858

You need a loop to reinitialize the file upload library so that you can process some other images that are uploaded by the user.

Let's say a user uploaded 2 images. Then that means you need to put the code that initialize the file upload library and do the file upload inside that loop.

for ($i = 0; $i < 2; $i++)
{
     // Change the config here if necessary
     $this->upload->initialize($config);
     // Call do_upload() here
}

Upvotes: 4

Related Questions