Codeigniter:不允许您尝试上传的文件类型

时间:2017-03-26 04:22:30

标签: php codeigniter upload

我正在使用CI最新版本。 上传JPG文件时出错。我使用了https://www.codeigniter.com/userguide3/libraries/file_uploading.html中的代码和Multiple files upload in Codeigniter

中的一些代码

控制器

$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'; }

查看

<?php echo form_open(base_url().'report/send', 'method="post" enctype="multipart/form-data"', $hidden);?>
<input type="file" class="hidden" name="gambar1"></form>

当我尝试上传JPG文件时,它会为我提供不允许您尝试上传的文件类型。 任何的想法?

5 个答案:

答案 0 :(得分:2)

这可能会有所帮助

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

打印错误,看看它之后返回的内容

答案 1 :(得分:1)

<强>解: 我试着把它上传到服务器。但它仍然给我同样的错误。然后我发现了这个:Codeigniter : The filetype you are attempting to upload is not allowed. Yesterday it was fine “我最近的时间是将我的核心Codeiginter版本从2.x升级到3.x.我更新了系统目录的内容,但没有更新application / config。我没有发现这个事实application / config / mimes.php文件略有不同 - 较新版本返回一个数组,而旧版本将其作为var包含。修复程序是在较新版本的mimes.php中复制“< / p>

答案 2 :(得分:0)

我遇到了同样的问题,我发现错误来自上传的图片。 如果它们最初是.png文件而我自己更改为.jpg,我会遇到这种错误。更改任何文件的mime类型时也会发生同样的情况。 我所做的是放回原来的mime类型,它工作得很好。

答案 3 :(得分:0)

我遇到了同样的问题.......并且找到了太多解决方案,但没有任何解决方案对我有用........然后我尝试使用不同的方法进行上传,并且可以正常工作。 ..... CI版本为3.1.10

这是您的解决方案

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

答案 4 :(得分:0)

对于 PDF,CI3 mimes 数组在 config/mimes.php 文件中没有“application/octet-stream”。在 pdf 密钥中添加“应用程序/八位字节流”。工作正常。

相关问题