Skip to content
Advertisement

Trying to upload multiple resized images in codeigniter php

I am trying to upload multiple images to a folder and save an encrypted image name in db. However, the code I have written, resizes the image, but saves original image name in database.

Example: if the encrypted original file is abc.jpg, the resized one is xyz.jpg. The db saves abc.jpg.

for($i=0; $i<$cpt; $i++)
    {
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];

        //new code
        $config['upload_path'] = './uploads/advert_images';
        $target_path = './uploads/advert_images/thumbs';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1000000'; //limit 1 mb
        $config['remove_spaces'] = true;
        $config['overwrite'] = false;
        $config['encrypt_name'] = TRUE;
        $config['max_width'] = '5000';// image max width
        $config['max_height'] = '5000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->do_upload('userfile');
        $fileName = $_FILES['userfile']['name'];
        $data = array('upload_data' => $this->upload->data());
        if(!$this->upload->do_upload('userfile'))
        {
            $error = array('upload_error' => $this->upload->display_errors());
            $this->session->set_flashdata('error',  $error['upload_error']);
            echo $files['userfile']['name'][$i].' '.$error['upload_error']; exit;

        } // resize code
        $path=$data['upload_data']['full_path'];
        $q['name']=$data['upload_data']['file_name'];
        $configi['image_library'] = 'gd2';
        $configi['source_image']   = $path;
        $configi['new_image']   = $target_path;
        $configi['maintain_ratio'] = FALSE;
        $configi['width']  = 300; // new size
        $configi['height'] = 300;
        $this->load->library('image_lib');
        $this->image_lib->initialize($configi);
        $this->image_lib->resize();
        unlink($path);

        $insert[$i]['picture_file_name']=$this->upload->data('file_name');;

        $insert[$i]['add_id']=$last_insert_id;
    }

    $response=$this->db->insert_batch('product_pictures',$insert);

Advertisement

Answer

in your File Upload Preferences you also need, in addition to encrypt_name(), set file_name() as well:

If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type. If no extension is provided in the original file_name will be used.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement