Awais Goshi
Awais Goshi

Reputation: 19

upload multiple pictures in codeigniter

HTML Code

<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" size="20" />
<input type="file" name="file2" size="20" />
<input type="submit" value="upload" />
</form>

Controller code

public function do_upload()
    {
            $pic1 = "Name One";
            $pic2 = "Name Two";
            $RealName = array('file1', 'file2' );
            $ChangeName = array($pic1, $pic2 );
            $arrlength = count($ChangeName);

           for($x = 0; $x < $arrlength; $x++)
           {
            $config['upload_path']          = './uploads/';
            $config['file_name']          = $ChangeName[$x];
            $config['allowed_types']        = 'gif|jpg|jpeg|png';
            $config['max_size']             = 1909900;
            $this->load->library('upload', $config);
            $this->upload->do_upload($RealName[$x]);
            echo $ChangeName[$x]; echo "<br>";
            echo $RealName[$x]; echo "<br>";
           }
    }

Trying to upload multiple pictures. Code runs correctly but I'm facing some problems in saving all pictures. The problem is saving all pictures with same name (Name One).

Upvotes: 0

Views: 806

Answers (1)

curiosity
curiosity

Reputation: 844

try this one out. this code is I am using...

 private function upload_files($path, $title, $files)
{
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'jpg|gif|png',
        'overwrite'     => 1,                       
    );

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

    $images = array();

    foreach ($files['name'] as $key => $image) {
        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $fileName = $title .'_'. $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            return false;
        }
    }

    return $images;
}

Upvotes: 1

Related Questions