【问题标题】:CodeIgniter 3: "You have not specified any allowed file types. The filetype you are attempting to upload is not allowed."CodeIgniter 3:“您没有指定任何允许的文件类型。您尝试上传的文件类型是不允许的。”
【发布时间】:2018-01-24 16:22:06
【问题描述】:

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

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

我在 StackOverflow 上搜索并尝试了许多解决方案,但没有一个有效,这是我的代码:

views/screenshots.php

<form method="post" enctype="multipart/form-data" action="{baseurl}/upload/screenshot">
                    <input type="hidden" name="serverid" value="{id}" />
                    <label>Carica uno Screenshot</label>
                    <br />
                    <label class="custom-file" id="customFile" for="screenshot">
                        <input type="file" class="custom-file-input" id="screenshot" name="screenshot" aria-describedby="fileHelp">
                        <span class="custom-file-control form-control-file"></span>
                    </label>
                    <br />
                    <button type="submit" class="btn btn-secondary">Carica file</button>
                </form>

config/upload.php

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

controllers/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']);
    }

}

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

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] => )

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

我在 Windows 10(PHP 版本 7.2.0)上使用 XAMPP 7.2.0。

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

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

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

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

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

    根据official document

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

    【讨论】:

    • 我已经尝试过“*”和“png|jpg|jpeg”等格式,但没有。
    【解决方案2】:

    替换:

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

    与:

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

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

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

    参数:

    $config (array) – 首选项

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

    【讨论】:

    • 他很可能应该从config.php 中省略$config['allowed_types'] = '*';,因为默认情况下所有类型都是允许的,我认为这就是* 他想要的
    猜你喜欢
    • 2017-08-18
    • 2012-04-06
    • 2012-04-22
    • 1970-01-01
    • 2011-11-21
    • 2011-06-08
    • 1970-01-01
    • 2014-12-30
    • 2019-05-20
    相关资源
    最近更新 更多