1

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);
Vickel
  • 7,879
  • 6
  • 35
  • 56
dinesh
  • 71
  • 1
  • 7
  • So you want to store the resized image name in the db? Why don't you have the resized image name to be like `abc-resized.jpg` or `resized/abc.jpg` instead of `xyz.jpg` and get the resized image name from the original image name stored in the db – Phani Rithvij Jul 07 '20 at 03:41

1 Answers1

0

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.

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • This worked fine, also how to delete teh file in the location $config['upload_path'] = './uploads/advert_images'; anfter the resizing is done, i dont want to keep the files. I have put as "unlink($path);" but the files are still there – dinesh Jul 07 '20 at 07:09
  • @dinesh *How to delete the file*: I'd suggest that you ask a new question, to treat this issue separately. – Vickel Jul 07 '20 at 11:20
  • i have posted a new question https://stackoverflow.com/questions/62774239/how-to-delete-files-after-uploading-in-php-codeigniter – dinesh Jul 07 '20 at 11:28