CodeIgniter "The filetype you are attempting to upload is not allowed."
Asked Answered
G

7

6

I was searching a lot and found many questions regarding this problem, unfortunately none of answers did help me.

I'm trying to upload a png image, and I'm receiving the following error:

The filetype you are attempting to upload is not allowed.

I was following this CI guide to build my code: http://codeigniter.com/user_guide/libraries/file_uploading.html

Here is what I got:

view file:

[..]
   <?= form_open_multipart() ?>
   <input type="file" name="userfile" size="20" />
   <br /><br />
   <input type="submit" value="upload" />
   <?= form_close() ?>
[..]

My controller:

    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '100';
    $config['max_width']     = '1024';
    $config['max_height']    = '768';


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

        $xx = array('upload_data' => $this->upload->data());
        $mimetype= $xx['upload_data']['file_type'];

        var_dump('Mime: ' . $mimetype);
        var_dump($_FILES);

        if ( !$this->upload->do_upload())
        {
            Notice::add($this->upload->display_errors(), 'error');
        }
        else
        {
            $data['upload_data'] = $this->upload->data();
        }

As you can see I'm tryin to var_dump the mime type and result is empty.

When I do var_dump($_FILES) it looks like everything is fine:

array(1) { ["userfile"]=> array(5) { ["name"]=> string(14) "imageofm.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(18) "/var/tmp/php5cDAZJ" ["error"]=> int(0) ["size"]=> int(358) } }

Also, I got 'png' => array('image/png', 'image/x-png'), line, in my config/mimes.php.

However, it does happend for all images (haven't tried yet other extensions).

I'd appreciate every help attempt.

Geraud answered 9/4, 2012 at 19:50 Comment(0)
P
7

Just edit the mimes.php file in application/config/mimes.php and replace the line for the csv by this one:

'csv' => array('application/vnd.ms-excel', 'text/anytext', 'text/plain', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel')
Procedure answered 22/7, 2013 at 15:22 Comment(1)
There goes 10, not my type of file (mp3) but help me solve my problemStephaniastephanie
N
4

Add ‘text/plain’ to the CSV array in config/mimes.php to $mimes arrays

Nert answered 27/12, 2013 at 10:45 Comment(1)
Thank you - this solved my problem. My problem was related directly to CSV files, with the same error message.Hibbler
L
1

Replacing the codeigniter 2.1.0 system/libraries/upload.php with 2.1.2 version upload.php library solves the problem. Hope this helps

Loredo answered 1/9, 2012 at 13:9 Comment(0)
C
1
$this->load->library('upload');  

$this->upload->set_allowed_types('*'); 


class MY_Upload extends CI_Upload {  

    function is_allowed_filetype() {  

        if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))  
        {  
            $this->set_error('upload_no_file_types');  
            return FALSE;  
        }  

        if (in_array("*", $this->allowed_types))  
        {  
            return TRUE;  
        }  

        $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');  

        foreach ($this->allowed_types as $val)  
        {  
            $mime = $this->mimes_types(strtolower($val));  

            // Images get some additional checks  
            if (in_array($val, $image_types))  
            {  
                if (getimagesize($this->file_temp) === FALSE)  
                {  
                    return FALSE;  
                }  
            }  

            if (is_array($mime))  
            {  
                if (in_array($this->file_type, $mime, TRUE))  
                {  
                    return TRUE;  
                }  
            }  
            else  
            {  
                if ($mime == $this->file_type)  
                {  
                    return TRUE;  
                }  
            }  
        }  

        return FALSE;  

    }  

}  
Chanell answered 22/7, 2013 at 15:46 Comment(0)
S
0

another solution is to enable extension=php_fileinfo.dll in php.ini

Scofflaw answered 10/4, 2013 at 9:51 Comment(0)
Y
0

i just add this line in mime.php on line 34 and pptx is now uploading. test it on real server

'pptx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),

Yahweh answered 15/8, 2013 at 22:49 Comment(0)
D
0

Actually, in my case, I created a blob object from a canvas by void canvas.toBlob(callback, mimeType, qualityArgument) method So the blob file does NOT have its real name (which has an extension), it's just only 'blob'. So I have to use the legacy way to upload the file, instead of an 'upload' library:

move_uploaded_file ( $file["tmp_name"], $target_file )
Dither answered 5/7, 2018 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.