Yudhistira Bayu
Yudhistira Bayu

Reputation: 293

Codeigniter: The filetype you are attempting to upload is not allowed

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?

Upvotes: 1

Views: 23073

Answers (5)

Rakesh
Rakesh

Reputation: 53

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.

Upvotes: 0

Milan R Dhameliya
Milan R Dhameliya

Reputation: 89

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)
            );
        }
    }

Upvotes: 1

Geni Jaho
Geni Jaho

Reputation: 75

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.

Upvotes: 2

Yudhistira Bayu
Yudhistira Bayu

Reputation: 293

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"

Upvotes: 2

Mudassar Khani
Mudassar Khani

Reputation: 1479

This might help

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

print errors and see what it returns after this

Upvotes: 2

Related Questions