上传表单 在ThinkPHP中使用上传功能无需进行特别处理。例如,下面是一个带有附件上传的表单提交: form action = __URL__/upload enctype = multipart/form-data method = post input type = text name = name / input type = file name = photo / input typ
上传表单
在ThinkPHP中使用上传功能无需进行特别处理。例如,下面是一个带有附件上传的表单提交:
1 2 3 4 5 6 7 8 9 | <ol>
<li><code><span><form>
<span>action</span><span>=</span><span> "__URL__/upload" </span><span>enctype</span><span>=</span><span> "multipart/form-data" </span><span>method</span><span>=</span><span> "post" </span><span>></span>
</form></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> "text" </span><span>name</span><span>=</span><span> "name" </span><span>/></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> "file" </span><span>name</span><span>=</span><span> "photo" </span><span>/></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> "submit" </span><span>value</span><span>=</span><span> "提交" </span><span>></span></span></code></li>
<li><code></code></li>
</ol>
|
登入後複製
注意,要使用上传功能 你的表单需要设置 enctype="multipart/form-data"
多文件上传支持
如果需要使用多个文件上传,只需要修改表单,把
1 | <ol><li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo' </span><span>></span></span></code></li></ol>
|
登入後複製
改为
1 2 3 4 5 | <ol>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo1' </span><span>></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo2' </span><span>></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo3' </span><span>></span></span></code></li>
</ol>
|
登入後複製
或者
1 2 3 4 5 | <ol>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo[]' </span><span>></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo[]' </span><span>></span></span></code></li>
<li><code><span><input><span>type</span><span>=</span><span> 'file' </span><span>name</span><span>=</span><span> 'photo[]' </span><span>></span></span></code></li>
</ol>
|
登入後複製
两种方式的多附件上传系统的文件上传类都可以自动识别。
上传操作
ThinkPHP文件上传操作使用Think\Upload
类,假设前面的表单提交到当前控制器的upload方法,我们来看下upload方法的实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <ol>
<li><code><span> public </span><span> function </span><span> upload</span><span>(){</span></code></li>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>();</span><span>
<li><code><span> $upload </span><span>-></span><span>maxSize </span><span>=</span><span>3145728</span><span>;</span><span>
<li><code><span> $upload </span><span>-></span><span>exts </span><span>=</span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>);</span><span>
<li><code><span> $upload </span><span>-></span><span>rootPath </span><span>=</span><span> './Uploads/' </span><span>;</span><span>
<li><code><span> $upload </span><span>-></span><span>savePath </span><span>=</span><span> '' </span><span>;</span><span>
<li><code><span>
<li><code><span> $info </span><span>=</span><span> $upload </span><span>-></span><span>upload</span><span>();</span></code></li>
<li><code><span> if </span><span>(!</span><span> $info </span><span>)</span><span>{</span><span>
<li><code><span> $this </span><span>-></span><span>error</span><span>(</span><span> $upload </span><span>-></span><span>getError</span><span>());</span></code></li>
<li><code><span>}</span><span> else </span><span>{</span><span>
<li><code><span> $this </span><span>-></span><span>success</span><span>(</span><span> '上传成功!' </span><span>);</span></code></li>
<li><code><span>}</span></code></li>
<li><code>}</code></li>
</ol>
|
登入後複製
上传类对图片文件的上传安全做了支持,如果企图上传非法的图像文件,系统会提示 非法图像文件
。 为了更好的使用上传功能,建议你的服务器开启finfo
模块支持
上传参数
在上传操作之前,我们可以对上传的属性进行一些设置,Upload类支持的属性设置包括:
属性 |
描述 |
maxSize |
文件上传的最大文件大小(以字节为单位),0为不限大小 |
rootPath |
文件上传保存的根路径 |
savePath |
文件上传的保存路径(相对于根路径) |
saveName |
上传文件的保存规则,支持数组和字符串方式定义 |
saveExt |
上传文件的保存后缀,不设置的话使用原文件后缀 |
replace |
存在同名文件是否是覆盖,默认为false |
exts |
允许上传的文件后缀(留空为不限制),使用数组或者逗号分隔的字符串设置,默认为空 |
mimes |
允许上传的文件类型(留空为不限制),使用数组或者逗号分隔的字符串设置,默认为空 |
autoSub |
自动使用子目录保存上传文件 默认为true |
subName |
子目录创建方式,采用数组或者字符串方式定义 |
hash |
是否生成文件的hash编码 默认为true |
callback |
检测文件是否存在回调,如果存在返回文件信息数组 |
上面的属性可以通过两种方式传入:
实例化传入
我们可以在实例化的时候直接传入参数数组,例如:
1 2 3 4 5 6 7 8 9 10 11 12 | <ol>
<li><code><span> $config </span><span>=</span><span> array </span><span>(</span></code></li>
<li><code><span> 'maxSize' </span><span>=></span><span>3145728</span><span>,</span></code></li>
<li><code><span> 'rootPath' </span><span>=></span><span> './Uploads/' </span><span>,</span></code></li>
<li><code><span> 'savePath' </span><span>=></span><span> '' </span><span>,</span></code></li>
<li><code><span> 'saveName' </span><span>=></span><span> array </span><span>(</span><span> 'uniqid' </span><span>,</span><span> '' </span><span>),</span></code></li>
<li><code><span> 'exts' </span><span>=></span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>),</span></code></li>
<li><code><span> 'autoSub' </span><span>=></span><span>true</span><span>,</span></code></li>
<li><code><span> 'subName' </span><span>=></span><span> array </span><span>(</span><span> 'date' </span><span>,</span><span> 'Ymd' </span><span>),</span></code></li>
<li><code>);</code></li>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>(</span><span> $config </span><span>);</span><span>
</ol>
|
登入後複製
关于saveName和subName的使用后面我们会有详细的描述。
动态赋值
支持在实例化后动态赋值上传参数,例如:
1 2 3 4 5 6 7 8 9 10 | <ol>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>();</span><span>
<li><code><span> $upload </span><span>-></span><span>maxSize </span><span>=</span><span>3145728</span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>rootPath </span><span>=</span><span> './Uploads/' </span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>savePath </span><span>=</span><span> '' </span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> array </span><span>(</span><span> 'uniqid' </span><span>,</span><span> '' </span><span>);</span></code></li>
<li><code><span> $upload </span><span>-></span><span>exts </span><span>=</span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>);</span></code></li>
<li><code><span> $upload </span><span>-></span><span>autoSub </span><span>=</span><span>true</span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>subName </span><span>=</span><span> array </span><span>(</span><span> 'date' </span><span>,</span><span> 'Ymd' </span><span>);</span></code></li>
</ol>
|
登入後複製
上面的设置和实例化传入的效果是一致的。
上传文件信息
设置好上传的参数后,就可以调用Think\Upload
类的upload方法进行附件上传,如果失败,返回false,并且用getError方法获取错误提示信息;如果上传成功,就返回成功上传的文件信息数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <ol>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>();</span><span>
<li><code><span> $upload </span><span>-></span><span>maxSize </span><span>=</span><span>3145728</span><span>;</span><span>
<li><code><span> $upload </span><span>-></span><span>exts </span><span>=</span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>);</span><span>
<li><code><span> $upload </span><span>-></span><span>rootPath </span><span>=</span><span> './Uploads/' </span><span>;</span><span>
<li><code><span> $upload </span><span>-></span><span>savePath </span><span>=</span><span> '' </span><span>;</span><span>
<li><code>
<li><code><span> $info </span><span>=</span><span> $upload </span><span>-></span><span>upload</span><span>();</span></code></li>
<li><code><span> if </span><span>(!</span><span> $info </span><span>)</span><span>{</span><span>
<li><code><span> $this </span><span>-></span><span>error</span><span>(</span><span> $upload </span><span>-></span><span>getError</span><span>());</span></code></li>
<li><code><span>}</span><span> else </span><span>{</span><span>
<li><code><span> foreach </span><span>(</span><span> $info </span><span> as </span><span> $file </span><span>){</span></code></li>
<li><code><span> echo $file </span><span>[</span><span> 'savepath' </span><span>].</span><span> $file </span><span>[</span><span> 'savename' </span><span>];</span></code></li>
<li><code><span>}</span></code></li>
<li><code>}</code></li>
</ol>
|
登入後複製
每个文件信息又是一个记录了下面信息的数组,包括:
属性 |
描述 |
key |
附件上传的表单名称 |
savepath |
上传文件的保存路径 |
name |
上传文件的原始名称 |
savename |
上传文件的保存名称 |
size |
上传文件的大小 |
type |
上传文件的MIME类型 |
ext |
上传文件的后缀类型 |
md5 |
上传文件的md5哈希验证字符串 仅当hash设置开启后有效 |
sha1 |
上传文件的sha1哈希验证字符串 仅当hash设置开启后有效 |
文件上传成功后,就可以使用这些文件信息来进行其他的数据操作,例如保存到当前数据表或者单独的附件数据表。
例如,下面表示把上传信息保存到数据表的字段:
1 2 3 4 5 6 7 8 9 | <ol>
<li><code><span> $model </span><span>=</span><span> M</span><span>(</span><span> 'Photo' </span><span>);</span></code></li>
<li><code>
<li><code><span> $info </span><span>=</span><span> $upload </span><span>-></span><span>upload</span><span>();</span></code></li>
<li><code>
<li><code><span> $data </span><span>[</span><span> 'photo' </span><span>]</span><span>=</span><span> $info </span><span>[</span><span>0</span><span>][</span><span> 'savename' </span><span>];</span></code></li>
<li><code><span> $data </span><span>[</span><span> 'create_time' </span><span>]</span><span>=</span><span> NOW_TIME</span><span>;</span></code></li>
<li><code><span> $model </span><span>-></span><span>add</span><span>(</span><span> $data </span><span>);</span></code></li>
</ol>
|
登入後複製
单文件上传
upload方法支持多文件上传,有时候,我们只需要上传一个文件,就可以使用Upload类提供的uploadOne方法上传单个文件,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <ol>
<li><code><span> public </span><span> function </span><span> upload</span><span>(){</span></code></li>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>();</span><span>
<li><code><span> $upload </span><span>-></span><span>maxSize </span><span>=</span><span>3145728</span><span>;</span><span>
<li><code><span> $upload </span><span>-></span><span>exts </span><span>=</span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>);</span><span>
<li><code><span> $upload </span><span>-></span><span>rootPath </span><span>=</span><span> './Uploads/' </span><span>;</span><span>
<li><code><span>
<li><code><span> $info </span><span>=</span><span> $upload </span><span>-></span><span>uploadOne</span><span>(</span><span> $_FILES </span><span>[</span><span> 'photo1' </span><span>]);</span></code></li>
<li><code><span> if </span><span>(!</span><span> $info </span><span>)</span><span>{</span><span>
<li><code><span> $this </span><span>-></span><span>error</span><span>(</span><span> $upload </span><span>-></span><span>getError</span><span>());</span></code></li>
<li><code><span>}</span><span> else </span><span>{</span><span>
<li><code><span> echo $info </span><span>[</span><span> 'savepath' </span><span>].</span><span> $info </span><span>[</span><span> 'savename' </span><span>];</span></code></li>
<li><code><span>}</span></code></li>
<li><code>}</code></li>
</ol>
|
登入後複製
uploadOne方法上传成功后返回的文件信息和upload方法的区别是只有单个文件信息的一维数组。
上传文件的命名规则
上传文件的命名规则(saveName)用于确保文件不会产生冲突或者覆盖的情况。命名规则的定义可以根据你的业务逻辑来调整,不是固定的。例如, 如果你采用时间戳的方式来定义命名规范,那么在同时上传多个文件的时候可能产生冲突(因为同一秒内可以上传多个文件),因此你需要根据你的业务需求来设置 合适的上传命名规则。这里顺便来说下saveName参数的具体用法。
一、采用函数方式
如果传入的字符串是一个函数名,那么表示采用函数动态生成上传文件名(不包括文件后缀),例如:
1 2 3 4 5 6 | <ol>
<li><code>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> 'time' </span><span>;</span></code></li>
<li><code>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> 'com_create_guid' </span><span>;</span></code></li>
</ol>
|
登入後複製
也可以采用用户自定义函数
1 2 3 4 | <ol>
<li><code>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> 'myfun' </span><span>;</span></code></li>
</ol>
|
登入後複製
默认的命名规则设置是采用uniqid函数生成一个唯一的字符串序列。
saveName的值支持数组和字符串两种方式,如果是只有一个参数或者没有参数的函数,直接使用字符串设置即可,如果需要传入额外的参数,可以使用数组方式,例如:
1 2 3 4 5 6 | <ol>
<li><code>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> array </span><span>(</span><span> 'date' </span><span>,</span><span> 'Y-m-d' </span><span>);</span></code></li>
<li><code>
<li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> array </span><span>(</span><span> 'myFun' </span><span>,</span><span> array </span><span>(</span><span> '__FILE__' </span><span>,</span><span> 'val1' </span><span>,</span><span> 'val2' </span><span>));</span></code></li>
</ol>
|
登入後複製
如果需要使用上传的原始文件名,可以采用__FILE__传入,所以上面的定义规则,最终的结果是 myFun('上传文件名','val1','val2')
执行的结果。
二、直接设置上传文件名
如果传入的参数不是一个函数名,那么就会直接当做是上传文件名,例如:
1 | <ol><li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> time</span><span>().</span><span> '_' </span><span>.</span><span>mt_rand</span><span>();</span></code></li></ol>
|
登入後複製
表示上传的文件命名采用时间戳加一个随机数的组合字符串方式。
当然,如果觉得有必要,你还可以固定设置一个上传文件的命名规则,用于固定保存某个上传文件。
1 | <ol><li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> 'ThinkPHP' </span><span>;</span></code></li></ol>
|
登入後複製
三、保持上传文件名不变
如果你想保持上传的文件名不变,那么只需要设置命名规范为空即可,例如:
1 | <ol><li><code><span> $upload </span><span>-></span><span>saveName </span><span>=</span><span> '' </span><span>;</span></code></li></ol>
|
登入後複製
一般来说不建议保持不变,因为会导致相同的文件名上传后被覆盖的情况。
子目录保存
saveName只是用于设置文件的保存规则,不涉及到目录,如果希望对上传的文件分子目录保存,可以设置autoSub
和subName
参数来完成,例如:
1 2 3 4 5 | <ol>
<li><code>
<li><code><span> $upload </span><span>-></span><span>autoSub </span><span>=</span><span>true</span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>subName </span><span>=</span><span> array </span><span>(</span><span> 'date' </span><span>,</span><span> 'Ymd' </span><span>);</span></code></li>
</ol>
|
登入後複製
可以使用自定义函数来保存,例如:
1 2 3 4 5 | <ol>
<li><code>
<li><code><span> $upload </span><span>-></span><span>autoSub </span><span>=</span><span>true</span><span>;</span></code></li>
<li><code><span> $upload </span><span>-></span><span>subName </span><span>=</span><span> 'get_user_id' </span><span>;</span></code></li>
</ol>
|
登入後複製
和saveName参数一样,subName的定义可以采用数组和字符串的方式。
注意:如果get_user_id函数未定义的话,会直接以get_user_id字符串作为子目录的名称保存。
子目录保存和文件命名规则可以结合使用。
上传驱动
上传类可以支持不同的环境,通过相应的上传驱动来解决,默认情况下使用本地(Local)上传驱动,当然,你还可以设置当前默认的上传驱动类型,例如:
1 2 3 4 5 6 7 8 9 | <ol>
<li><code><span> 'FILE_UPLOAD_TYPE' </span><span>=></span><span> 'Ftp' </span><span>,</span></code></li>
<li><code><span> 'UPLOAD_TYPE_CONFIG' </span><span>=></span><span> array </span><span>(</span></code></li>
<li><code><span> 'host' </span><span>=></span><span> '192.168.1.200' </span><span>,</span><span>
<li><code><span> 'port' </span><span>=></span><span>21</span><span>,</span><span>
<li><code><span> 'timeout' </span><span>=></span><span>90</span><span>,</span><span>
<li><code><span> 'username' </span><span>=></span><span> 'ftp_user' </span><span>,</span><span>
<li><code><span> 'password' </span><span>=></span><span> 'ftp_pwd' </span><span>,</span><span>
</ol>
|
登入後複製
表示当前使用Ftp作为上传类的驱动,上传的文件会通过FTP传到指定的远程服务器。
也可以在实例化上传类的时候指定,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <ol>
<li><code><span> $config </span><span>=</span><span> array </span><span>(</span></code></li>
<li><code><span> 'maxSize' </span><span>=</span><span>3145728</span><span>,</span></code></li>
<li><code><span> 'rootPath' </span><span>=</span><span> './Uploads/' </span><span>,</span></code></li>
<li><code><span> 'savePath' </span><span>=</span><span> '' </span><span>,</span></code></li>
<li><code><span> 'saveName' </span><span>=</span><span> array </span><span>(</span><span> 'uniqid' </span><span>,</span><span> '' </span><span>),</span></code></li>
<li><code><span> 'exts' </span><span>=</span><span> array </span><span>(</span><span> 'jpg' </span><span>,</span><span> 'gif' </span><span>,</span><span> 'png' </span><span>,</span><span> 'jpeg' </span><span>),</span></code></li>
<li><code><span> 'autoSub' </span><span>=</span><span>true</span><span>,</span></code></li>
<li><code><span> 'subName' </span><span>=</span><span> array </span><span>(</span><span> 'date' </span><span>,</span><span> 'Ymd' </span><span>),</span></code></li>
<li><code>);</code></li>
<li><code><span> $ftpConfig </span><span>=</span><span> array </span><span>(</span></code></li>
<li><code><span> 'host' </span><span>=></span><span> '192.168.1.200' </span><span>,</span><span>
<li><code><span> 'port' </span><span>=></span><span>21</span><span>,</span><span>
<li><code><span> 'timeout' </span><span>=></span><span>90</span><span>,</span><span>
<li><code><span> 'username' </span><span>=></span><span> 'ftp_user' </span><span>,</span><span>
<li><code><span> 'password' </span><span>=></span><span> 'ftp_pwd' </span><span>,</span><span>
<li> </li>
<li><code><span> $upload </span><span>=</span><span> new </span><span> \Think\Upload</span><span>(</span><span> $config </span><span>,</span><span> 'Ftp' </span><span>,</span><span> $ftpConfig </span><span>);</span><span>
</ol>
|
登入後複製