【问题标题】:Can't upload Image CodeIgniter, filetype error无法上传图像 CodeIgniter,文件类型错误
【发布时间】:2011-04-30 17:01:45
【问题描述】:

我写了下面的代码:

function upld_logo()
     {
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';


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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['msg']='Sorry! Cant Upload' . $error['error'];
           $data['main_content']='message';
           $this->load->view('template',$data);


        }   
        else
        {
            //$data = array('upload_data' => $this->upload->data());

            $names = $this->upload->data();
            $k=$names['file_name'];


            $data['msg']='Your Logo Successfully Uploaded. ';
           $data['main_content']='message';
           $this->load->view('template',$data); 
        }

     }

当我上传名为 logo.gif 的图像文件时,它会显示错误消息:

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

【问题讨论】:

  • 它可以在我的本地电脑上运行,但是上传时显示上面的错误
  • 您的消息不应该是“抱歉!无法上传:文件类型....”吗?
  • 您是否检查过 Web 服务器访问和错误日​​志中是否存在任何 PHP 或权限错误?
  • 检查另一张图片,您的 logo.gif 可能不是 .gif

标签: php codeigniter file-upload


【解决方案1】:

在你的系统->libraries->Upload.php line 200中找到这段代码

$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type); 

并替换为:

$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);

【讨论】:

  • 这对我有用。奇怪的是,这在 localhost 服务器上不是问题,而是在现场。假设它可能与 linux 相关。
【解决方案2】:

是的,我也遇到了同样的错误。 如果你改变$[allowed_types] = "*"(允许所有文件) 你会发现上传成功但是你的图片文件没有类型!

我认为 CI 库可能有问题?

【讨论】:

    【解决方案3】:

    我在同样的问题上花了一些时间。您可能会发现 codeigniters 默认上传库正在使用几个已弃用的 PHP 函数来查找文件 mime 类型....祝您好运,我仍在尝试自己修补它。

    【讨论】:

    • 在进一步调查之后,在某些 Apache/PHP 设置上似乎不可用的 Upload 类中的函数是:finfo_filemime_content_type(其中只有一个似乎已被弃用此时 PHP 的更高版本,另一个只是不知何故丢失)
    猜你喜欢
    • 2012-06-01
    • 2014-10-17
    • 1970-01-01
    • 2011-07-09
    • 2018-06-28
    • 2012-04-14
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多