CodeIgniter 3:“您尚未指定任何允许的文件类型。不允许您尝试上传的文件类型。”

时间:2018-01-24 16:22:06

标签: php codeigniter file-upload

我正在尝试使用CodeIgniter“上传”库进行文件上传,但每次我尝试上传时都会收到此错误:

“您尚未指定任何允许的文件类型。不允许您尝试上传的文件类型。”

我已经在StackOverflow上搜索并尝试了很多解决方案,但它们都没有工作,这是我的代码:

视图/ screenshots.php

$config['allowed_types'] = '*';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;

配置/ upload.php的

public function screenshot()
{
    /* Upload library config */
    $config['upload_path'] = './files/screenshots/';
    $this->upload->initialize($config);

    /* Upload and check if it's failed */
    if (!$this->upload->do_upload('screenshot')) {
        $error = array('error' => $this->upload->display_errors());
        echo json_encode($error);
        print_r($this->upload->data());
    } else {
        /* Making an array with all the data of the upload */
        $upload_data = $this->upload->data();

        /* Making an array to pass to the Screenshot_model */
        $screenshot = array(
            'id_server' => $this->input->post('serverid'),
            'id_user' => $this->session->userdata('userid'),
            'image_url' => $upload_data('file_name')
        );

        /* Saving the Screenshot info into the database */
        $this->Screenshot_model->addScreen($screenshot);

        /* Redirect to Server edit */
        redirect('/screenshots/'.$screenshot['id_server']);
    }

}

控制器/ upload.php的

Array ( [file_name] => download.png [file_type] => image/png [file_path] => C:/xampp/htdocs/gameparade/files/screenshots/ [full_path] => C:/xampp/htdocs/gameparade/files/screenshots/download.png [raw_name] => download [orig_name] => [client_name] => download.png [file_ext] => .png [file_size] => 1806 [is_image] => 1 [image_width] => [image_height] => [image_type] => [image_size_str] => )

print_r($ this-> upload-> data())结果

{{1}}

我正在自动加载库。我的CodeIgniter版本是3.1.6。我也试过使用form_open_multipart();在视图中,但没有任何变化。

我在Windows 10(PHP V7.2.0)上使用XAMPP 7.2.0。

2 个答案:

答案 0 :(得分:2)

manual没有说'*'可以用于所有类型。所以最好尝试改变这个:

$config['allowed_types'] = '*';

$config['allowed_types'] = 'gif|jpg|png';

然后尝试使用其中一种文件类型。

根据official document

  

allowed_types default = Noneoptions = None mime类型对应的类型   您允许上传的文件。通常可以使用文件扩展名   作为哑剧类型。用管道分隔多种类型。

答案 1 :(得分:1)

替换:

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

使用:

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

来自文档(https://www.codeigniter.com/userguide3/libraries/file_uploading.html):

  

初始化([array $ config = array()[,$ reset = TRUE]])

     

参数:

     

$ config(array) - 首选项

     

$ reset(bool) - 是否将首选项($ config中未提供)重置为默认值