Skip to content
Advertisement

Upload Images in two different paths Codeigniter

I’ll try to upload an Images into two different path, but it always uploaded in single path only, here’s my code in a model :

public function save()
{
    $post = $this->input->post();
    $this->product_id = uniqid();
    $this->name = $post["name"];
    $this->price_awal = $post["price_awal"];
    $this->price = $post["price"];
    $this->image = $this->_uploadImage();
    $this->image2 = $this->_uploadImage2('./upload/');
    $this->jenis_produk = $post["jenis_produk"];
    $this->warna = $post["warna"];
    $this->description = $post["description"];
    $this->db->insert($this->_table, $this);
}

The upload function (also in the model)

private function _uploadImage()
{
    $config['upload_path']          = './upload/product/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB
    // $config['max_width']            = 1024;
    // $config['max_height']           = 768;

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

    if ($this->upload->do_upload('image')) {
        return $this->upload->data("file_name");
    }
    
    return "default.jpg";
}

private function _uploadImage2($path)
{
$config['upload_path']          = $path . '/' . $this->product_id;
$config['allowed_types']        = 'gif|jpg|png';
$config['file_name']            = $this->product_id;
$config['overwrite']            = true;
$config['max_size']             = 1024; // 1MB

if (!is_dir($path . $this->product_id)) {
    mkdir($path . $this->product_id, 0777, TRUE);
}

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

if ($this->upload->do_upload('image')) {
    return $this->upload->data("file_name");
}

return "default.jpg";
}

Only the _uploadImage function worked, but the _uploadImage2 not work

Can you tell me what’s wrong?

Advertisement

Answer

The problem is that you have already initialize the upload library, so you have to re-initialize it.

Plus you should try to nest the functions, so they will be called in chain, or you don’t want just use $this->upload->initialize($config); in the second function:

private function _uploadImage($path)
{
    $config['upload_path']          = './upload/product/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB
    // $config['max_width']            = 1024;
    // $config['max_height']           = 768;

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

    if ($this->upload->do_upload('image')) {
        return $this->_uploadImage2($path);
    }
    
    return "default.jpg";
}

private function _uploadImage2($path)
{
$config['upload_path']          = $path . '/' . $this->product_id;
$config['allowed_types']        = 'gif|jpg|png';
$config['file_name']            = $this->product_id;
$config['overwrite']            = true;
$config['max_size']             = 1024; // 1MB

if (!is_dir($path . $this->product_id)) {
    mkdir($path . $this->product_id, 0777, TRUE);
}

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

if ($this->upload->do_upload('image')) {
    return $this->upload->data("file_name");
}

return "default.jpg";
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement