douer9399 2018-01-24 16:22
浏览 65

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

I'm attempting to do a File Upload using the CodeIgniter "upload" library, but everytime I try to upload something I get this error:

"You have not specified any allowed file types. The filetype you are attempting to upload is not allowed."

I've searched and tried many solutions here on StackOverflow but none of them are working, here's my code:

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()) result

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

I'm autoloading the library. My version of CodeIgniter is 3.1.6. I've also tried to use form_open_multipart(); in the view, but nothing changes.

I'm using XAMPP 7.2.0 on Windows 10 (PHP Version 7.2.0).

  • 写回答

2条回答 默认 最新

  • douwei8295 2018-01-24 16:29
    关注

    Replace:

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

    with:

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

    From the docs (https://www.codeigniter.com/userguide3/libraries/file_uploading.html):

    initialize([array $config = array()[, $reset = TRUE]])

    Parameters:

    $config (array) – Preferences

    $reset (bool) – Whether to reset preferences (that are not provided in $config) to their defaults

    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码