1

I'm using CI latest version. Got error while uploading a JPG files. I use the code from here https://www.codeigniter.com/userguide3/libraries/file_uploading.html and a little bit from Multiple files upload in Codeigniter

The Controller:

$config = array(
'upload_path'   => 'path to upload',
'allowed_types' => 'jpg|gif|png|jpeg',
'overwrite'     => 0,
'max_size'      => 1024,                       
);
$this->load->library('upload', $config);
$this->upload->initialize($config); // Make sure it has been initialized

if (!$this->upload->do_upload('gambar1')){
    $error = array('error' => $this->upload->display_errors());
    return $error;
}else{ echo 'success'; }

The View:

<?php echo form_open(base_url().'report/send', 'method="post" enctype="multipart/form-data"', $hidden);?>
<input type="file" class="hidden" name="gambar1"></form>

When I'm trying to upload JPG files, it gives me The filetype you are attempting to upload is not allowed. Any idea?

Community
  • 1
  • 1
Yudhistira Bayu
  • 293
  • 1
  • 3
  • 13
  • Try using form_open_multipart() and `'upload_path' => './uploads/',` –  Mar 26 '17 at 04:47

5 Answers5

2

This might help

'allowed_types' => 'jpg|gif|png|jpeg|JPG|PNG',

print errors and see what it returns after this

Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39
2

SOLUTION: I've try to upload it to server. But it still gives me the same error. Then i found this: Codeigniter : The filetype you are attempting to upload is not allowed. Yesterday it was fine "The most recent time I have had this is when I upgraded my core Codeiginter version from 2.x to 3.x. I updated the contents of the system directory, but not application/config. I didn't spot the fact that the application/config/mimes.php file is slightly different - the newer version returns an array whereas the old one included it as a var. The fix was to copy in the newer version of mimes.php"

Community
  • 1
  • 1
Yudhistira Bayu
  • 293
  • 1
  • 3
  • 13
  • I have the idential mimes. In fact I have the identical versions on localhost and online. They both worked a while back. Just now, I can't upload the Excel file online. Any clue? Thanks! – itsols Jun 07 '17 at 04:26
  • @itsols I ended up using `move_uploaded_file()` with my custom validation for file extensions. – nice_dev Sep 19 '19 at 13:10
2

I had the same problem and I figured out that the error came from the uploaded pictures. If they were originally .png files and I changed them myself to .jpg, I would get this kind of error. The same happened when changing the mime type of any file. What I did was put back the original mime type and it worked just fine.

Geni Jaho
  • 75
  • 1
  • 7
  • 2
    I had the same problem where Dropzone.js is transforming them on the client and uploading .png as .jpg. Codeigniter complained about the filetype even though I'd set the `$config["allowed_types"] = 'gif|jpg|png|jpeg';`. Setting the `"allowed_types" = '*'` gets CI to shut up. – ourmandave Aug 04 '17 at 14:57
  • @ourmandave . thank man. this helped me. i try to allow txt file not working. so i changed to * and it works – Sras Jul 06 '21 at 13:46
1

i am faced same problem.......and find too much solution but no any solution worked for me........then i was try to use different method to upload and that is worked......CI Version is 3.1.10

here is your solution

$report_pdf = "";

    if (!empty($_FILES) && isset($_FILES["report_pdf"])) {

        $ext = pathinfo($_FILES['report_pdf']['name'], PATHINFO_EXTENSION);
        $new_name = 'report_pdf_' . time() . '_' . $this->get_random_string(4) . '.' . $ext;
        $file_url = "uploads/" . $new_name;


        $config['file_name'] = $new_name . "." . $ext;
        $config['upload_path'] = "uploads/";
        $config['allowed_types'] = '*';

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

        $_FILES['userfile2']['name'] = $new_name . "." . $ext; //$_FILES['songs']['name'][$i];
        $_FILES['userfile2']['type'] = $_FILES['report_pdf']['type'];
        $_FILES['userfile2']['tmp_name'] = $_FILES['report_pdf']['tmp_name'];
        $_FILES['userfile2']['error'] = $_FILES['report_pdf']['error'];
        $_FILES['userfile2']['size'] = $_FILES['report_pdf']['size'];
        $this->upload->initialize($config);
        if ($this->upload->do_upload('userfile2')) {

            $finfo = $this->upload->data();
            $report_pdf = $file_url;
        } else {
            $error = $this->upload->display_errors();
            $result = array(
                'msg' => strip_tags($error)
            );
        }
    }
0

For PDF, CI3 mimes array don't have "application/octet-stream" in the config/mimes.php file. Add "application/octet-stream" in the pdf key. Works fine.

Rakesh
  • 53
  • 7