CodeIgniter 用户指南 版本 1.6.3

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

文件上传类

CodeIgniter 的文件上传类允许文件被上传。您可以设置指定上传某类型的文件及指定大小的文件。

处理过程

上传文件普遍的过程:

这里有一个简短的教程来显示这个过程。此后你将会找到相关的参考信息。

创建上传表单

运用文本编辑器创建一个名为upload_form.php的文件,复制以下代码并保存在applications/views/目录里:

你会看到这里运用到了一个表单辅助函数来创建表单的开始标签,文件上传需要一个 multipart form,因为这个表单辅助函数为你创建了一个合适的语句。你还会看到我们运用了一个$error变量,当用户提交该表单出现错误时会显示相关的出错信息。

上传成功的页面

运用文本编辑器创建一个名为upload_success.php的文件。复制以下代码保存到applications/views/目录里:

控制器

运用文本编辑器,创建一个名为upload.php的控制器.复制以下代码并保存到applications/controllers/目录里:

上传文件目录

你还需要一个目标文件夹来存储上传的图片。在根目录上创建一个名为uploads的文件并设置该文件的属性为 777。(即可读写)

提交表单

要提交你的表单,输入类似如下的URL:

example.com/index.php/upload/

你将看到一个上传表单,任选一张(jpg, gif,或者png)图片进行提交. 如果你在控制器里设置的路径是正确的,它将开始工作。

 

参考指南

初始化文件上传类

与CodeIgniter的其它一些类相似,文件上传类用$this->load->library函数在控制器里进行初始化:

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

一旦文件上传类被加载,对象将通过如下方法来引用:$this->upload

偏好设置

与其它库类似,你将根据你的偏好设置来控制要被上传的文件,在控制器里,你建立了如下的偏好设置:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);

以上偏好设置将被完全执行。以下是所有偏好设置参数的描述。

偏好设置参数

以下偏好设置参数是可用的。当你没有特别指定偏好设置参数时,默认值如下:

Preference Default Value Options Description
upload_path None None The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.
allowed_types None None The mime types corresponding to the types of files you allow to be uploaded. Usually the file extension can be used as the mime type. Separate multiple types with a pipe.
overwrite FALSE TRUE/FALSE (boolean) 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 The maximum size (in kilobytes) that the file can be. Set to zero for no limit. Note: Most PHP installations have their own limit, as specified in the php.ini file. Usually 2 MB (or 2048 KB) by default.
max_width 0 None The maximum width (in pixels) that the file can be. Set to zero for no limit.
max_height 0 None The maximum height (in pixels) that the file can be. Set to zero for no limit.
encrypt_name FALSE TRUE/FALSE (boolean) If set to TRUE the file name will be converted to a random encrypted string. This can be useful if you would like the file saved with a name that can not be discerned by the person uploading it. 当 overwrite 为 FALSE 时,此选项才起作用。
remove_spaces TRUE TRUE/FALSE (boolean) If set to TRUE, any spaces in the file name will be converted to underscores. This is recommended.

在配置文件里设置偏好设置参数

如果你不愿意应用如上方法进行偏好设置,你可能用一个配置文件来取代它。简单创建一个名为upload.php的文件,添加 $config数组到该文件里,然后保存文件到:config/upload.php,它将被自动加载。当你把配置参数保存到该文件里,你不需要用$this->upload->initialize函数进行手动加载。

运用到的函数

以下函数被运用

$this->upload->do_upload()

根据你的偏好配置参数执行操作。注意:默认情况下上传的文件来自于提交表单里名为userfile的文件域,并且该表单必须是 "multipart"类型:

<form method="post" action="some_action" enctype="multipart/form-data" />

如果你想在执行do_upload函数之前自定义自己的文件域名称,可通过以下方法实现:

$field_name = "some_field_name";
$this->upload->do_upload($field_name)

$this->upload->display_errors()

如果do_upload()返回假,显示错误信息。此函数不会自动输出,而是返回数据,所以你可以按你的要求安排。

格式化错误

上面的函数默认使用<p>标记错误信息。你可以像这样设置自己的分隔符。

$this->upload->display_errors('<p>', '</p>');

$this->upload->data()

这是一个辅助函数,它返回你上传文件的所以相关信息的数组。

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

解释

这里是对上面数组项的解释。

ItemDescription
file_name The name of the file that was uploaded including the file extension.
file_type The file's Mime type
file_path The absolute server path to the file
full_path The absolute server path including the file name
raw_name The file name without the extension
orig_name The original file name. This is only useful if you use the encrypted name option.
file_ext The file extension with period
file_size 图像大小,单位是kb
is_image 是否是图像。 1 =是图像。 0 = 不是图像。
image_width 图像宽度.
image_heigth 图像高度
image_type Image type. Typically the file extension without the period.
image_size_str 一个包含width和height的字符串。用于放在一个img标签里。

 

翻译贡献者: Hex, lishen2, szlinz, thankwsx
最后修改: 2008-11-07 10:09:54