Search code examples
phpimagecodeigniter-3

Image mime type and extension is correct but get failed upload


i have image with extension .jpeg and mime type is image/jpeg.. and this is the var_dump() from the uploaded image using file upload (input type="file")

array (size=5)
 'name' => string 'WhatsApp Image 2023-02-01 at 17.07.41.jpeg' (length=42)
 'type' => string 'image/jpeg' (length=10)
 'tmp_name' => string 'D:\xampp\tmp\phpDAF2.tmp' (length=24)
 'error' => int 0
 'size' => int 140484

other references said that maybe the mimes.php (oh and yes i am using codeigniter 3) and this is the mimes.php for jpeg extension, here's the mimes.php line on jpeg

'gif'   =>  'image/gif',
'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
'jpg'   =>  array('image/jpeg', 'image/pjpeg'),

i'm confused while i do this upload attached the code

$config['upload_path'] = './uploads/patient_img/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['file_name'] = strtolower($patient->get_name());
$config['overwrite'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['image_library'] = 'gd2';

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

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

$error = [];
$listing_filename = [];
foreach ($list_image as $row) {
    if ($_FILES['image_' . $row]['name'] != '' && is_null($this->input->post('existing_image_' . $row))) {
        if (!$this->upload->do_upload('image_' . $row)) {
            $error[] = $this->upload->display_errors();
            print_r($this->upload->display_errors());
            die();
            break;
        } else {
            $listing_filename[] = $this->upload->data('file_name');
        }
    }
}

and i get the CI Upload error "The filetype you are attempting to upload is not allowed."

anybody knows what is going on? because i can't find the answer.. thanks a lot


Solution

  • i found the solution!

    turns out that i have using combination of string for the filename, when i double check it the name contain "." (dot) that makes the uploaded image already has "."

    let's say i input "this is. a name" and the uploaded image filename will be "this_is._a_name" that cause extension isn't valid

    so the solution is simply replace the "." using regex