codeigniter 的文件上传类允许文件被上传。您可以设置指定上传某类型的文件及指定大小的文件。
上传文件普遍的过程:
下面是表单:
<form method="post" action="<?=base_url()?>admin/img_upload/" enctype="multipart/form-data" /> <div style="margin:0 0 0.5em 0em;"> <input type="file" name="userfile" size="20" class="button" /> <input type="submit" value=" 上传 " class="button" /> </div> </form>
然后是下面是上传类:
public function img_upload()
{
$this->load->helper('url');
$config['upload_path'] = './images/'.date('Ym', time()).'/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = date('Y_m_d', time()).'_'.sprintf('%02d', rand(0,99));
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
| 偏好设置 | 默认值 | 选项 | 描述 |
|---|---|---|---|
| upload_path | None | None | 文件上传路径。该路径必须是可写的,相对路径和绝对路径均可以。 |
| allowed_types | None | None | 允许上传文件的MIME类型;通常文件扩展名可以做为MIME类型. 允许多个类型用竖线‘|’分开 |
| file_name | None | 想要使用的文件名 |
如果设置了这个参数,CodeIgniter 将根据这里设置的文件名来对上传的文件进行重命名。文件名中的扩展名也必须是允许的文件类型。 立即学习“PHP免费学习笔记(深入)”; |
| overwrite | FALSE | TRUE/FALSE (boolean) | 是否覆盖。该参数为TRUE时,如果上传文件时碰到重名文件,将会把原文件覆盖;如果该参数为FALSE,CI将会在新文件的文件名后面加一个数字。If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. If set to false, a number will be appended to the filename if another with the same name exists. |
| max_size | 0 | None | 允许上传文件大小的最大值(以K为单位)。该参数为0则不限制。注意:通常PHP也有这项限制,可以在php.ini文件中指定。通常默认为2MB。 |
| max_width | 0 | None | 上传文件的宽度最大值(像素为单位)。0为不限制。 |
| max_height | 0 | None | 上传文件的高度最大值(像素为单位)。0为不限制。 |
| max_filename | 0 | None | 文件名的最大长度。0为不限制。 |
| encrypt_name | FALSE | TRUE/FALSE (boolean) | 是否重命名文件。如果该参数为TRUE,上传的文件将被重命名为随机的加密字符串。当你想让文件上传者也不能区分自己上传的文件的文件名时,是非常有用的。当 overwrite 为 FALSE 时,此选项才起作用。 |
| remove_spaces | TRUE | TRUE/FALSE (boolean) | 参数为TRUE时,文件名中的空格将被替换为下划线。推荐使用。 |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号