此篇文章主要讲解如何封装一个php的用于文件上传下载的函数,可以用于本demo中的头像上传功能,顺便讲解一下多文件上传的方式。如果对于文件上传特别熟悉可以跳过此节。 还是使用之前那个界面。 addUser.php: !doctype html html head meta charset = "utf-8
此篇文章主要讲解如何封装一个php的用于文件上传下载的函数,可以用于本demo中的头像上传功能,顺便讲解一下多文件上传的方式。如果对于文件上传特别熟悉可以跳过此节。
还是使用之前那个界面。
addUser.php:
<code><span> <span>html</span>></span> <span>head</span>> <span>meta</span> <span>charset</span>=<span>"utf-8"</span>> <span>title</span>><span><span>title</span>></span> <span><span>head</span>></span> <span>body</span>> <span>H3</span>>添加管理员<span><span>H3</span>></span> <span>FORM</span> <span>action</span>=<span>"doAdminAction.php?act=addUser"</span> <span>method</span>=<span>"post"</span>> <span>TABLE</span> <span>width</span>=<span>"60%"</span> <span>border</span>=<span>"1"</span> <span>cellpadding</span>=<span>"5"</span> <span>cellspacing</span>=<span>"0"</span> <span>bgcolor</span>=<span>"#cccccc"</span>> <span>tr</span>> <span>TD</span> <span>align</span>=<span>"right"</span>>用户名称<span><span>TD</span>></span> <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"username"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span> <span><span>tr</span>></span> <span>tr</span>> <span>TD</span> <span>align</span>=<span>"right"</span>>用户密码<span><span>TD</span>></span> <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"password"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span> <span><span>tr</span>></span> <span>tr</span>> <span>TD</span> <span>align</span>=<span>"right"</span>>用户邮箱<span><span>TD</span>></span> <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"email"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span> <span><span>tr</span>></span> <span>tr</span>> <span>td</span> <span>align</span>=<span>"right"</span>>性别<span><span>td</span>></span> <span>td</span>><span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"1"</span> <span>checked</span>=<span>"checked"</span>/>男 <span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"2"</span> />女 <span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"3"</span> />保密 <span><span>td</span>></span> <span><span>tr</span>></span> <span>tr</span>> <span>td</span> <span>align</span>=<span>"right"</span>>头像<span><span>td</span>></span> <span>td</span>><span>input</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"face"</span> /><span><span>td</span>></span> <span><span>tr</span>></span> <span>tr</span>> <span>TD</span> <span>colspan</span>=<span>"2"</span>><span>input</span> <span>type</span>=<span>"submit"</span> <span>value</span>=<span>"添加用户"</span>/><span><span>TD</span>></span> <span><span>tr</span>></span> <span><span>FORM</span>></span> <span><span>body</span>></span> <span><span>html</span>></span></code>
这里是文件目录:
在表单提交的doAdminAction.php中,只需要增 加move_uploaded_file($_FILES["face"]["tmp_name"], <br>
"upload/" . $_FILES["face"]["name"]);
这一行代码,即可把客户端上传的文件放在服务器中的对应的地方。但是这样的代码有一些问题
(1)如果在admin不存在upload的文件夹,文件上传不上去。
(2)对于上传的文件的类型没有进行检验,如果出现病毒文件会比较危险。
(3)如果上传了与upload文件夹中的文件名称相同的文件,原来的文件就会被替换掉。
(4)对于文件上传可能会出现的一些错误没有进行处理,例如文件大小超过了2M,(php ini 文件中默认规定的上传限制大小为2M)
下面是解决方案:
(1)首先判断upload的文件夹是否存在,如果不存在则建立
<code><span>$path</span>=<span>"upload/"</span>; <span>//</span>如果路径不存在就建立的路径上 <span>if</span>(!file_exists(<span>$path</span>)) { mkdir(<span>$path</span>,<span>0777</span>,<span>true</span>); } </code>
(2)使用strtolower(end(explode(“.”, $filename)));获得文件的拓展名
然后判断拓展名是否在所需要的文件类型中。
(3)使用md5(uniqid(microtime(true),true));得到一个唯一的字符串与之前得到的文件拓展名连接在一起,作为上传文件的文件名。
(4)现在开始编写doAdminAction的代码,第四个问题将在代码中讲解。
<code><span><span><?php </span> <span>require_once</span> <span>'../include.php'</span>; <span>//print_r($_FILES["face"]);</span> <span>$path</span>=<span>"upload/"</span>; <span>//如果路径不存在就建立的路径上</span> <span>if</span>(!file_exists(<span>$path</span>)) { mkdir(<span>$path</span>,<span>0777</span>,<span>true</span>); } <span>//允许的类型</span> <span>$allowExt</span>=<span>array</span>(<span>"gif"</span>,<span>"jpeg"</span>,<span>"png"</span>,<span>"jpg"</span>,<span>"wbmp"</span>); <span>$filename</span>=<span>$_FILES</span>[<span>"face"</span>][<span>"name"</span>]; <span>$ext</span>=getExt(<span>$filename</span>); <span>if</span>(!in_array(<span>$ext</span>,<span>$allowExt</span>)){ <span>exit</span>(<span>"非法文件类型"</span>); } <span>//得到了唯一的文件名的文件</span> <span>$filename</span>=getUniName().<span>"."</span>.<span>$ext</span>; <span>if</span>(move_uploaded_file(<span>$_FILES</span>[<span>"face"</span>][<span>"tmp_name"</span>], <span>$path</span>.<span>$filename</span>)) { <span>echo</span> <span>"文件上传成功"</span>; } <span>else</span> { <span>switch</span>(<span>$_FILES</span>[<span>"face"</span>][<span>'error'</span>]){ <span>case</span> <span>1</span>: <span>$mes</span>=<span>"超过了配置文件上传文件的大小"</span>;<span>//UPLOAD_ERR_INI_SIZE</span> <span>break</span>; <span>case</span> <span>2</span>: <span>$mes</span>=<span>"超过了表单设置上传文件的大小"</span>; <span>//UPLOAD_ERR_FORM_SIZE</span> <span>break</span>; <span>case</span> <span>3</span>: <span>$mes</span>=<span>"文件部分被上传"</span>;<span>//UPLOAD_ERR_PARTIAL</span> <span>break</span>; <span>case</span> <span>4</span>: <span>$mes</span>=<span>"没有文件被上传"</span>;<span>//UPLOAD_ERR_NO_FILE</span> <span>break</span>; <span>case</span> <span>6</span>: <span>$mes</span>=<span>"没有找到临时目录"</span>;<span>//UPLOAD_ERR_NO_TMP_DIR</span> <span>break</span>; <span>case</span> <span>7</span>: <span>$mes</span>=<span>"文件不可写"</span>;<span>//UPLOAD_ERR_CANT_WRITE;</span> <span>break</span>; <span>case</span> <span>8</span>: <span>$mes</span>=<span>"由于PHP的扩展程序中断了文件上传"</span>;<span>//UPLOAD_ERR_EXTENSION</span> <span>break</span>; } <span>echo</span> <span>$mes</span>; } <span>?></span></span></span></code>
如果发生了出现的错误,则这些错误会被打印出来。至此单文件上传的内容讲解完毕,现在将上述的代码封装为函数。
<code><span><?php </span> <span><span>function</span> <span>uploadFile</span><span>(<span>$path</span>=<span>"upload/"</span>,<span>$allowExt</span>=array<span>(<span>"gif"</span>,<span>"jpeg"</span>,<span>"png"</span>,<span>"jpg"</span>,<span>"wbmp"</span>)</span>,<span>$imgFlag</span>=true)</span> {</span> <span>//如果路径不存在就建立的路径上</span> <span>if</span>(!file_exists(<span>$path</span>)) { mkdir(<span>$path</span>,<span>0777</span>,<span>true</span>); } <span>//允许的类型</span> <span>$filename</span>=<span>$_FILES</span>[<span>"face"</span>][<span>"name"</span>]; <span>$ext</span>=getExt(<span>$filename</span>); <span>if</span>(!in_array(<span>$ext</span>,<span>$allowExt</span>)){ <span>exit</span>(<span>"非法文件类型"</span>); } <span>if</span>(<span>$imgFlag</span>){ <span>//如何验证图片是否是一个真正的图片类型</span> <span>//getimagesize($filename):验证文件是否是图片类型 正确返回数组错误返回false</span> <span>$info</span>=getimagesize(<span>$tmp_name</span>); <span>//var_dump($info);exit;</span> <span>if</span>(!<span>$info</span>){ <span>exit</span>(<span>"不是真正的图片类型"</span>); } } <span>//得到了唯一的文件名的文件</span> <span>$filename</span>=getUniName().<span>"."</span>.<span>$ext</span>; <span>if</span>(move_uploaded_file(<span>$_FILES</span>[<span>"face"</span>][<span>"tmp_name"</span>], <span>$path</span>.<span>$filename</span>)) { <span>echo</span> <span>"文件上传成功"</span>; } <span>else</span> { <span>switch</span>(<span>$_FILES</span>[<span>"face"</span>][<span>'error'</span>]){ <span>case</span> <span>1</span>: <span>$mes</span>=<span>"超过了配置文件上传文件的大小"</span>;<span>//UPLOAD_ERR_INI_SIZE</span> <span>break</span>; <span>case</span> <span>2</span>: <span>$mes</span>=<span>"超过了表单设置上传文件的大小"</span>; <span>//UPLOAD_ERR_FORM_SIZE</span> <span>break</span>; <span>case</span> <span>3</span>: <span>$mes</span>=<span>"文件部分被上传"</span>;<span>//UPLOAD_ERR_PARTIAL</span> <span>break</span>; <span>case</span> <span>4</span>: <span>$mes</span>=<span>"没有文件被上传"</span>;<span>//UPLOAD_ERR_NO_FILE</span> <span>break</span>; <span>case</span> <span>6</span>: <span>$mes</span>=<span>"没有找到临时目录"</span>;<span>//UPLOAD_ERR_NO_TMP_DIR</span> <span>break</span>; <span>case</span> <span>7</span>: <span>$mes</span>=<span>"文件不可写"</span>;<span>//UPLOAD_ERR_CANT_WRITE;</span> <span>break</span>; <span>case</span> <span>8</span>: <span>$mes</span>=<span>"由于PHP的扩展程序中断了文件上传"</span>;<span>//UPLOAD_ERR_EXTENSION</span> <span>break</span>; } <span>echo</span> <span>$mes</span>; } }</span></code>
多文件上传分为两种情况
(1)多个单文件上传
<code><span> <span>html</span>></span> <span>head</span>> <span>meta</span> <span>charset</span>=<span>"utf-8"</span>> <span>title</span>><span><span>title</span>></span> <span><span>head</span>></span> <span>body</span>> <span><!-- 表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作. --></span> <span>FORM</span> <span>action</span>=<span>"doAction2.php"</span> <span>method</span>=<span>"post"</span> <span>enctype</span>=<span>"multipart/form-data"</span>> <span>INPUT</span> <span>type</span>=<span>"hidden"</span> <span>name</span>=<span>"MAX_FILE_SIZE"</span> <span>value</span>=<span>"1048576"</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile1"</span>/><span>br</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile2"</span>/><span>br</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile3"</span>/><span>br</span>/> <span>input</span> <span>type</span>=<span>"submit"</span> <span>value</span>=<span>"上传"</span>/> <span><span>FORM</span>></span> <span><span>body</span>></span> <span><span>html</span>></span></code>
这种形式相当于上传了多个单文件,在doAction2.php中print_r($_FILES);得到
<code><span>Array</span> ( [myFile1] => <span>Array</span> ( [name] => touxiang.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\php79E3.tmp [error] => <span>0</span> [size] => <span>28747</span> ) [myFile2] => <span>Array</span> ( [name] => 文章.png [type] => image/png [tmp_name] => D:\xampp\tmp\php7A03.tmp [error] => <span>0</span> [size] => <span>11655</span> ) [myFile3] => <span>Array</span> ( [name] => 增加.png [type] => image/png [tmp_name] => D:\xampp\tmp\php7A04.tmp [error] => <span>0</span> [size] => <span>7439</span> ) )</code>
(2)多文件上传
<code><span> <span>html</span>></span> <span>head</span>> <span>meta</span> <span>charset</span>=<span>"utf-8"</span>> <span>title</span>><span><span>title</span>></span> <span><span>head</span>></span> <span>body</span>> <span><!-- 表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作. --></span> <span>FORM</span> <span>action</span>=<span>"doAction2.php"</span> <span>method</span>=<span>"post"</span> <span>enctype</span>=<span>"multipart/form-data"</span>> <span>INPUT</span> <span>type</span>=<span>"hidden"</span> <span>name</span>=<span>"MAX_FILE_SIZE"</span> <span>value</span>=<span>"1048576"</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile[]"</span>/><span>br</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile[]"</span>/><span>br</span>/> 请选择上传文件:<span>INPUT</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"myFile[]"</span>/><span>br</span>/> <span>input</span> <span>type</span>=<span>"submit"</span> <span>value</span>=<span>"上传"</span>/> <span><span>FORM</span>></span> <span><span>body</span>></span> <span><span>html</span>></span></code>
同样print_r($_files)一下
<code> <span>Array</span> ( [myFile] => <span>Array</span> ( [name] => <span>Array</span> ( [<span>0</span>] => touxiang.jpg [<span>1</span>] => 文章.png [<span>2</span>] => 增加.png ) [type] => <span>Array</span> ( [<span>0</span>] => image/jpeg [<span>1</span>] => image/png [<span>2</span>] => image/png ) [tmp_name] => <span>Array</span> ( [<span>0</span>] => D:\xampp\tmp\php1EC.tmp [<span>1</span>] => D:\xampp\tmp\php1ED.tmp [<span>2</span>] => D:\xampp\tmp\php1EE.tmp ) [error] => <span>Array</span> ( [<span>0</span>] => <span>0</span> [<span>1</span>] => <span>0</span> [<span>2</span>] => <span>0</span> ) [size] => <span>Array</span> ( [<span>0</span>] => <span>28747</span> [<span>1</span>] => <span>11655</span> [<span>2</span>] => <span>7439</span> ) ) ) </code>
发现两种方式得到的数组有很多的不同。
对于第一种的情况(相当于一个文件数组)我们可以
<code><span>foreach</span> (<span>$_FILES</span> <span>as</span> <span>$var</span>) { <span>//对于每一个文件进行了文件上传操作</span> <span>$info</span>=uploadFile(<span>$var</span>); <span>echo</span> <span>$info</span>; }</code>
对于第二种情况我们可以将所示的三维数组转换为第一种情况所示的二维数组即可。
<code>foreach(<span>$_FILES</span>[myFile][name] as <span>$key</span>=><span>$var</span> ) { <span>$files</span>[<span>$key</span>][name]=<span>$_FILES</span>[myFile][name][<span>$key</span>]; <span>$files</span>[<span>$key</span>][type]=<span>$_FILES</span>[myFile][type][<span>$key</span>]; <span>$files</span>[<span>$key</span>][tmp_name]=<span>$_FILES</span>[myFile][tmp_name][<span>$key</span>]; <span>$files</span>[<span>$key</span>][<span>error</span>]=<span>$_FILES</span>[myFile][<span>error</span>][<span>$key</span>]; <span>$files</span>[<span>$key</span>][<span>size</span>]=<span>$_FILES</span>[myFile][<span>size</span>][<span>$key</span>]; } print_r(<span>$files</span>);</code>
结果
<code><span>Array</span> ( [<span>0</span>] => <span>Array</span> ( [name] => touxiang.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\php3227.tmp [error] => <span>0</span> [size] => <span>28747</span> ) [<span>1</span>] => <span>Array</span> ( [name] => 文章.png [type] => image/png [tmp_name] => D:\xampp\tmp\php3228.tmp [error] => <span>0</span> [size] => <span>11655</span> ) [<span>2</span>] => <span>Array</span> ( [name] => 增加.png [type] => image/png [tmp_name] => D:\xampp\tmp\php3229.tmp [error] => <span>0</span> [size] => <span>7439</span> ) )</code>
成功了,现在我们开始封装一个文件上传函数,这个函数呢既可以处理单文件上传又可以处理多文件上传。然后我们只需要编写数组处理的这部分,然后调用upload()函数即可。
<code><span><?php </span> <span>require_once</span> <span>"../include.php"</span>; <span>//用于转换数组</span> <span><span>function</span> <span>buildInfo</span><span>()</span> {</span> <span>foreach</span>(<span>$_FILES</span> <span>as</span> <span>$file</span>) { <span>$i</span>=<span>0</span>; <span>if</span>(is_string(<span>$file</span>[<span>'name'</span>])) { <span>$files</span>[<span>$i</span>]=<span>$file</span>; <span>$i</span>++; } <span>else</span> { <span>foreach</span>(<span>$file</span>[name] <span>as</span> <span>$key</span>=><span>$var</span>) { <span>$files</span>[<span>$key</span>][name]=<span>$file</span>[name][<span>$key</span>]; <span>$files</span>[<span>$key</span>][type]=<span>$file</span>[type][<span>$key</span>]; <span>$files</span>[<span>$key</span>][tmp_name]=<span>$file</span>[tmp_name][<span>$key</span>]; <span>$files</span>[<span>$key</span>][error]=<span>$file</span>[error][<span>$key</span>]; <span>$files</span>[<span>$key</span>][size]=<span>$file</span>[size][<span>$key</span>]; } } } <span>return</span> <span>$files</span>; } <span>//用于上传</span> <span><span>function</span> <span>uploadFile</span><span>(<span>$path</span>=<span>"upload/"</span>,<span>$allowExt</span>=array<span>(<span>"gif"</span>,<span>"jpeg"</span>,<span>"png"</span>,<span>"jpg"</span>,<span>"wbmp"</span>)</span>)</span> {</span> <span>//如果路径不存在就建立的路径上</span> <span>if</span>(!file_exists(<span>$path</span>)) { mkdir(<span>$path</span>,<span>0777</span>,<span>true</span>); } <span>$files</span>=buildInfo(); <span>foreach</span>(<span>$files</span> <span>as</span> <span>$file</span>) { <span>//允许的类型</span> <span>$filename</span>=<span>$file</span>[<span>"name"</span>]; <span>$ext</span>=getExt(<span>$filename</span>); <span>if</span>(!in_array(<span>$ext</span>,<span>$allowExt</span>)){ <span>exit</span>(<span>"非法文件类型"</span>); } <span>//校验是否是一个真正的图片类型</span> <span>if</span>(<span>$imgFlag</span>){ <span>if</span>(!getimagesize(<span>$file</span>[<span>'tmp_name'</span>])){ <span>exit</span>(<span>"不是真正的图片类型"</span>); } } <span>//得到了唯一的文件名的文件</span> <span>$filename</span>=getUniName().<span>"."</span>.<span>$ext</span>; <span>if</span>(move_uploaded_file(<span>$file</span>[<span>"tmp_name"</span>], <span>$path</span>.<span>$filename</span>)) { <span>echo</span> <span>"文件上传成功"</span>; } <span>else</span> { <span>switch</span>(<span>$file</span>[<span>'error'</span>]){ <span>case</span> <span>1</span>: <span>$mes</span>=<span>"超过了配置文件上传文件的大小"</span>;<span>//UPLOAD_ERR_INI_SIZE</span> <span>break</span>; <span>case</span> <span>2</span>: <span>$mes</span>=<span>"超过了表单设置上传文件的大小"</span>; <span>//UPLOAD_ERR_FORM_SIZE</span> <span>break</span>; <span>case</span> <span>3</span>: <span>$mes</span>=<span>"文件部分被上传"</span>;<span>//UPLOAD_ERR_PARTIAL</span> <span>break</span>; <span>case</span> <span>4</span>: <span>$mes</span>=<span>"没有文件被上传"</span>;<span>//UPLOAD_ERR_NO_FILE</span> <span>break</span>; <span>case</span> <span>6</span>: <span>$mes</span>=<span>"没有找到临时目录"</span>;<span>//UPLOAD_ERR_NO_TMP_DIR</span> <span>break</span>; <span>case</span> <span>7</span>: <span>$mes</span>=<span>"文件不可写"</span>;<span>//UPLOAD_ERR_CANT_WRITE;</span> <span>break</span>; <span>case</span> <span>8</span>: <span>$mes</span>=<span>"由于PHP的扩展程序中断了文件上传"</span>;<span>//UPLOAD_ERR_EXTENSION</span> <span>break</span>; } <span>echo</span> <span>$mes</span>; } } }</span></code>
这段代码中还增加了对于上传图片是否为图片格式的检验,以及对于图片大小的检验,因为有些病毒文件会伪装为图片文件。