允许所有类型的文件上传
目标:
当使用ci的上传类的时候,你必须指明哪些文件类型允许上传。
复制代码
$this->load->library('upload'); $this->upload->set_allowed_types('jpg|jpeg|gif|png|zip');
如果你没有指明特定的上传类型,你将会从ci那里得到一个错误信息"Youhave not specified any allowed file types."
所有,默认的方式,将没有办法允许所有的文件上传。我们需要做一些小的改变来处理这个限制。我们设定“*”将能够运行所有类型的文件上传。
复制代码
$this->load->library('upload'); $this->upload->set_allowed_types('*');
技巧:
我们需要修改上传文件类。
创建文件:application/librarys/My_upload.php
class MY_Upload extends CI_Upload {
function is_allowed_filetype() {
if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
{
$this->set_error('upload_no_file_types');
return FALSE;
}
if (in_array("*", $this->allowed_types))
{
return TRUE;
}
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
foreach ($this->allowed_types as $val)
{
$mime = $this->mimes_types(strtolower($val));
// Images get some additional checks
if (in_array($val, $image_types))
{
if (getimagesize($this->file_temp) === FALSE)
{
return FALSE;
}
}
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
else
{
if ($mime == $this->file_type)
{
return TRUE;
}
}
}
return FALSE;
}
}