【问题标题】:Cannot upload image in CodeIgniter无法在 CodeIgniter 中上传图像
【发布时间】:2012-11-13 03:37:42
【问题描述】:

我无法在 Codeigniter 中上传图片。

它总是显示不允许您尝试上传的文件类型。

我的编码(控制器):

$this->load->helper(array('form', 'url'));
            $this->load->library('upload');
            $config['upload_path'] = "./public/uploadimg";
            $config['allowed_types'] = "*";
            $config['max_size'] = '10000';

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

            if ( ! $this->upload->do_upload('userfile'))
            {
                echo $this->upload->display_errors();
                //$this->load->view('upload_form', $error);
            }
            else
            {
                echo $this->upload->data($config);
                //$this->load->view('upload_success', $data);
            }

查看:

<form  enctype="multipart/form-data" action="http://192.1.100.1/site/test_image/upload/" method="post">
    <input type="file"  name="userfile" id="userfile"/><Br>
    <input type="submit" name="upload" id="upload">
</form>

问候,

【问题讨论】:

  • 您确定要向我们展示所有代码吗?触发 $this->set_error('upload_invalid_filetype'); 的唯一代码路径如果 is_allowed_filetype() 返回 false。根据您向我们展示的代码示例,allowed_types 应设置为 '*' 并且 is_allowed_filetype 应在第一个条件下返回 TRUE。
  • 是的,我所有的编码。那我该怎么办?
  • 您可以尝试修改 Upload.php 以添加一些调试输出,例如回显 allowed_types,确保将其设置为您的想法等...
  • 我应该在upload.php中更改哪一行?
  • 嗯,一个简单的方法是在第 586 行添加(如果您有相同版本的 CodeIgniter)回显 $this->allowed_types 以了解它包含的内容(在公共函数 is_allowed_filetype($ignore_mime =假))

标签: php codeigniter


【解决方案1】:

尝试明确设置您允许的类型。我记得以前遇到过同样的问题。

$config['allowed_types'] = 'docx|pdf|doc|gif|jpg|png|tiff'; 

【讨论】:

  • 我已经尝试过了,但没有工作。显示您尝试上传的文件类型不允许..
  • 您使用的是哪个版本的 CodeIgniter?我用 png 文件和 $config['allowed_types'] = 'png'; 尝试了你的代码它似乎有效。
【解决方案2】:
        $this->load->helper(array('form', 'url')); 
       if($this->input->post('upload')){ //submit name           

        $config['upload_path'] = "./public/uploadimg";
        $config['allowed_types'] = "png|jpg|jpeg";
        $config['max_size'] = '10000';
        $config['max_width'] = '1000';
        $config['max_height'] = '800';

        $this->load->library('upload',$config);

        if ( ! $this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
            //$this->load->view('upload_form', $error);
            }
        else{
            echo $this->upload->data($config);
            //$this->load->view('upload_success', $data);
            }
        }

并确保您的操作网址正确。对不起,我英语不好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多