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

て烟熏妆下的殇ゞ 提交于 2019-12-04 05:55:07

问题


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?


回答1:


This might help

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

print errors and see what it returns after this




回答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"




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/43024959/codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!