ホームページ  >  記事  >  php教程  >  PHP实现文件上传下载心在忙而已 - 韧还

PHP实现文件上传下载心在忙而已 - 韧还

WBOY
WBOYオリジナル
2016-05-20 13:15:221228ブラウズ

  这一周都没有写什么东西,是啊,一周时间都没有学习太多新的东西,除了开车。

妈蛋啊,天天中午去学车然后两周没有午觉的日子还是很崩溃的,加上之后工作压力带来的心忙,宝宝不开心啊。

不过,也是自己不是那么能吃苦吧。那天看到的那句话怎么说的来着,我痛恨自己,在简单和困难之前,选择了前者;在什么什么面前,balabala,不是很鸡汤,但有点触动,愿少点矫情和娇气。

PHP实现文件上传与下载——来自mooc网

一、上传原理与配置

1.1 原理

将客户端文件上传到服务器端,再将服务器端的文件(临时文件)移动到指定目录即可。

1.2 客户端配置

所需:表单页面(选择上传文件);

具体而言:发送方式为POST,添加enctype="multipart/form-data"属性,两者缺一不可(但是,优缺点并存,这里也限定了上传的方式和上传的文件之后的调用等方面,后面会说到)

<span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=UTF-8"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Insert title here<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="doAction.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
请选择您要上传的文件:
</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">php

</span><span style="color: #0000ff;">?></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span>

先是表单页面(请自动忽略前端问题。。。),关键就是form的属性;另外就是input 中用到了type="file"这一点(体现到php的强大的拓展等等)。

然后是doAction

<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">$_FILES:文件上传变量
//print_r($_FILES);</span>
<span style="color: #800080;">$filename</span>=<span style="color: #800080;">$_FILES</span>['myFile']['name'<span style="color: #000000;">];
</span><span style="color: #800080;">$type</span>=<span style="color: #800080;">$_FILES</span>['myFile']['type'<span style="color: #000000;">];
</span><span style="color: #800080;">$tmp_name</span>=<span style="color: #800080;">$_FILES</span>['myFile']['tmp_name'<span style="color: #000000;">];
</span><span style="color: #800080;">$size</span>=<span style="color: #800080;">$_FILES</span>['myFile']['size'<span style="color: #000000;">];
</span><span style="color: #800080;">$error</span>=<span style="color: #800080;">$_FILES</span>['myFile']['error'<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;">将服务器上的临时文件移动到指定位置
//方法一move_upload_file($tmp_name,$destination)
//move_uploaded_file($tmp_name, "uploads/".$filename);//文件夹应提前建立好,不然报错
//方法二copy($src,$des)
//以上两个函数都是成功返回真,否则返回false
//copy($tmp_name, "copies/".$filename);
//注意,不能两个方法都对临时文件进行操作,临时文件似乎操作完就没了,我们试试反过来</span>
<span style="color: #008080;">copy</span>(<span style="color: #800080;">$tmp_name</span>, "copies/".<span style="color: #800080;">$filename</span><span style="color: #000000;">);
</span><span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>, "uploads/".<span style="color: #800080;">$filename</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">能够实现,说明move那个函数基本上相当于剪切;copy就是copy,临时文件还在

//另外,错误信息也是不一样的,遇到错误可以查看或者直接报告给用户</span>
<span style="color: #0000ff;">if</span> (<span style="color: #800080;">$error</span>==0<span style="color: #000000;">) {
    </span><span style="color: #0000ff;">echo</span> "上传成功!"<span style="color: #000000;">;
}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
    </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$error</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">case</span> 1:
            <span style="color: #0000ff;">echo</span> "超过了上传文件的最大值,请上传2M以下文件"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 2:
            <span style="color: #0000ff;">echo</span> "上传文件过多,请一次上传20个及以下文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 3:
            <span style="color: #0000ff;">echo</span> "文件并未完全上传,请再次尝试!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 4:
            <span style="color: #0000ff;">echo</span> "未选择上传文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 5:
            <span style="color: #0000ff;">echo</span> "上传文件为0"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
    }
}</span>

 

先把print_r($_FILES)这个信息看一下

<span style="color: #000000;">Array
(
    [myFile] => Array
        (
            [name] => 梁博_简历.doc
            [type] => application/msword
            [tmp_name] => D:\wamp\tmp\php1D78.tmp
            [error] => 0
            [size] => 75776
        )

)</span>

所以得到的是个二维数组,该怎么用,都是基本的东西(其实我喜欢降维再用);

基本是一眼就懂的东西,不罗嗦,关键有两个:tmp_name临时文件名;error报错信息(代号,后面可以利用)

然后这里看一下doAction后面一部分,利用报错信息来反馈给用户,需要说明的是为什么报错,和报错信息是什么都;

1.3 关于报错

--报错原因

基本上都是超过或者不符合服务器关于上传文件的配置,那么服务器端配置有哪些呢?

先考虑上传我们用了什么?POST,upload

所以在php.ini中找这么几项:

file_upload:On

upload_tmp_dir=——临时文件保存目录;

upload_max_filesize=2M

max_file_uploads=20——允许一次上传的最大文件数量(注意和上面那个的区别,有没有size,别乱想)

post_max_size=8M——post方式发送数据的最大值

其他相关配置

max_exectuion_time=-1——最大执行时间,避免程序不好占用服务器资源;

max_input_time=60

max_input_nesting_level=64——输入嵌套深度;

memory_limit=128M——最大单线程的独立内存使用量

总之都是有关资源的配置。

--错误号

以下(偷懒)引自http://blog.sina.com.cn/s/blog_3cdfaea201008utf.html

UPLOAD_ERR_OK             值:0; 没有错误发生,文件上传成功。
  UPLOAD_ERR_INI_SIZE      值:1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
  UPLOAD_ERR_FORM_SIZE  值:2; 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
  UPLOAD_ERR_PARTIAL          值:3; 文件只有部分被上传。
  UPLOAD_ERR_NO_FILE          值:4; 没有文件被上传。  

注意:这个错误信息是第一步上传的信息,也就是上传到临时文件夹的情况,而不是move或者copy的情况。 

二、上传相关限制

2.1 客户端限制

<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="doAction2.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="MAX_FILE_SIZE"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="101321"</span> <span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:
</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile"</span><span style="color: #ff0000;"> accept</span><span style="color: #0000ff;">="image/jpeg,image/gif,text/html"</span><span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span></span></span></span></span></span>

 

这里用input的属性对上传文件的大小和类型进行了限制,但是个人感觉:一,html代码是“可见的”;二,常不起作用(没找到原因,但因为第一个我也想放弃它,知道就好。 

 2.2 服务器端限制

主要限制大小和类型,再有就是方式。

<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>('content-type:text/html;charset=utf-8'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">接受文件,临时文件信息</span>
<span style="color: #800080;">$fileinfo</span>=<span style="color: #800080;">$_FILES</span>["myFile"];<span style="color: #008000;">//</span><span style="color: #008000;">降维操作</span>
<span style="color: #800080;">$filename</span>=<span style="color: #800080;">$fileinfo</span>["name"<span style="color: #000000;">];
</span><span style="color: #800080;">$tmp_name</span>=<span style="color: #800080;">$fileinfo</span>["tmp_name"<span style="color: #000000;">];
</span><span style="color: #800080;">$size</span>=<span style="color: #800080;">$fileinfo</span>["size"<span style="color: #000000;">];
</span><span style="color: #800080;">$error</span>=<span style="color: #800080;">$fileinfo</span>["error"<span style="color: #000000;">];
</span><span style="color: #800080;">$type</span>=<span style="color: #800080;">$fileinfo</span>["type"<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;">服务器端设定限制</span>
<span style="color: #800080;">$maxsize</span>=10485760;<span style="color: #008000;">//</span><span style="color: #008000;">10M,10*1024*1024</span>
<span style="color: #800080;">$allowExt</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','tif');<span style="color: #008000;">//</span><span style="color: #008000;">允许上传的文件类型(拓展名</span>
<span style="color: #800080;">$ext</span>=<span style="color: #008080;">pathinfo</span>(<span style="color: #800080;">$filename</span>,PATHINFO_EXTENSION);<span style="color: #008000;">//</span><span style="color: #008000;">提取上传文件的拓展名

//目的信息</span>
<span style="color: #800080;">$path</span>="uploads"<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$path</span>)) {   <span style="color: #008000;">//</span><span style="color: #008000;">当目录不存在,就创建目录</span>
    <span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$path</span>,0777,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
    </span><span style="color: #008080;">chmod</span>(<span style="color: #800080;">$path</span>, 0777<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">$destination=$path."/".$filename;
//得到唯一的文件名!防止因为文件名相同而产生覆盖</span>
<span style="color: #800080;">$uniName</span>=<span style="color: #008080;">md5</span>(<span style="color: #008080;">uniqid</span>(<span style="color: #008080;">microtime</span>(<span style="color: #0000ff;">true</span>),<span style="color: #0000ff;">true</span>)).<span style="color: #800080;">$ext</span>;<span style="color: #008000;">//</span><span style="color: #008000;">md5加密,uniqid产生唯一id,microtime做前缀</span>


<span style="color: #0000ff;">if</span> (<span style="color: #800080;">$error</span>==0<span style="color: #000000;">) {
    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$size</span>><span style="color: #800080;">$maxsize</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">exit</span>("上传文件过大!"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$ext</span>, <span style="color: #800080;">$allowExt</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">exit</span>("非法文件类型"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">exit</span>("上传方式有误,请使用post方式"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (@<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>, <span style="color: #800080;">$uniName</span>)) {<span style="color: #008000;">//</span><span style="color: #008000;">@错误抑制符,不让用户看到警告</span>
        <span style="color: #0000ff;">echo</span> "文件".<span style="color: #800080;">$filename</span>."上传成功!"<span style="color: #000000;">;
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">echo</span> "文件".<span style="color: #800080;">$filename</span>."上传失败!"<span style="color: #000000;">;
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是否为真实图片(防止伪装成图片的病毒一类的</span>
    <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$tmp_name</span>)) {<span style="color: #008000;">//</span><span style="color: #008000;">getimagesize真实返回数组,否则返回false</span>
        <span style="color: #0000ff;">exit</span>("不是真正的图片类型"<span style="color: #000000;">);
    }

}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
    </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$error</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">case</span> 1:
            <span style="color: #0000ff;">echo</span> "超过了上传文件的最大值,请上传2M以下文件"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 2:
            <span style="color: #0000ff;">echo</span> "上传文件过多,请一次上传20个及以下文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 3:
            <span style="color: #0000ff;">echo</span> "文件并未完全上传,请再次尝试!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 4:
            <span style="color: #0000ff;">echo</span> "未选择上传文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 7:
            <span style="color: #0000ff;">echo</span> "没有临时文件夹"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
    }
}</span>

 

这里,具体实现都有注释,每一步其实都可以自己试试的,很有趣。

2.3 封装

函数

<span style="color: #000000;">php
</span><span style="color: #0000ff;">function</span> uploadFile(<span style="color: #800080;">$fileInfo</span>,<span style="color: #800080;">$path</span>,<span style="color: #800080;">$allowExt</span>,<span style="color: #800080;">$maxSize</span><span style="color: #000000;">){

</span><span style="color: #800080;">$filename</span>=<span style="color: #800080;">$fileInfo</span>["name"<span style="color: #000000;">];
</span><span style="color: #800080;">$tmp_name</span>=<span style="color: #800080;">$fileInfo</span>["tmp_name"<span style="color: #000000;">];
</span><span style="color: #800080;">$size</span>=<span style="color: #800080;">$fileInfo</span>["size"<span style="color: #000000;">];
</span><span style="color: #800080;">$error</span>=<span style="color: #800080;">$fileInfo</span>["error"<span style="color: #000000;">];
</span><span style="color: #800080;">$type</span>=<span style="color: #800080;">$fileInfo</span>["type"<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;">服务器端设定限制</span>

<span style="color: #800080;">$ext</span>=<span style="color: #008080;">pathinfo</span>(<span style="color: #800080;">$filename</span>,<span style="color: #000000;">PATHINFO_EXTENSION);

</span><span style="color: #008000;">//</span><span style="color: #008000;">目的信息</span>
<span style="color: #0000ff;">if</span> (!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)) {   
    </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$path</span>,0777,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
    </span><span style="color: #008080;">chmod</span>(<span style="color: #800080;">$path</span>, 0777<span style="color: #000000;">);
}
</span><span style="color: #800080;">$uniName</span>=<span style="color: #008080;">md5</span>(<span style="color: #008080;">uniqid</span>(<span style="color: #008080;">microtime</span>(<span style="color: #0000ff;">true</span>),<span style="color: #0000ff;">true</span>)).'.'.<span style="color: #800080;">$ext</span><span style="color: #000000;">;
</span><span style="color: #800080;">$destination</span>=<span style="color: #800080;">$path</span>."/".<span style="color: #800080;">$uniName</span><span style="color: #000000;">;


</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$error</span>==0<span style="color: #000000;">) {
    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$size</span>><span style="color: #800080;">$maxSize</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">exit</span>("上传文件过大!"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$ext</span>, <span style="color: #800080;">$allowExt</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">exit</span>("非法文件类型"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">exit</span>("上传方式有误,请使用post方式"<span style="color: #000000;">);
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是否为真实图片(防止伪装成图片的病毒一类的</span>
    <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$tmp_name</span>)) {<span style="color: #008000;">//</span><span style="color: #008000;">getimagesize真实返回数组,否则返回false</span>
        <span style="color: #0000ff;">exit</span>("不是真正的图片类型"<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">if</span> (@<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>, <span style="color: #800080;">$destination</span>)) {<span style="color: #008000;">//</span><span style="color: #008000;">@错误抑制符,不让用户看到警告</span>
        <span style="color: #0000ff;">echo</span> "文件".<span style="color: #800080;">$filename</span>."上传成功!"<span style="color: #000000;">;
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #0000ff;">echo</span> "文件".<span style="color: #800080;">$filename</span>."上传失败!"<span style="color: #000000;">;
    }
    

}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
    </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$error</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">case</span> 1:
            <span style="color: #0000ff;">echo</span> "超过了上传文件的最大值,请上传2M以下文件"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 2:
            <span style="color: #0000ff;">echo</span> "上传文件过多,请一次上传20个及以下文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 3:
            <span style="color: #0000ff;">echo</span> "文件并未完全上传,请再次尝试!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 4:
            <span style="color: #0000ff;">echo</span> "未选择上传文件!"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">case</span> 7:
            <span style="color: #0000ff;">echo</span> "没有临时文件夹"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
    }
}
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$destination</span><span style="color: #000000;">;
}</span>

 

调用

<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>('content-type:text/html;charset=utf-8'<span style="color: #000000;">);
</span><span style="color: #800080;">$fileInfo</span>=<span style="color: #800080;">$_FILES</span>["myFile"<span style="color: #000000;">];
</span><span style="color: #800080;">$maxSize</span>=10485760;<span style="color: #008000;">//</span><span style="color: #008000;">10M,10*1024*1024</span>
<span style="color: #800080;">$allowExt</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','tif'<span style="color: #000000;">);
</span><span style="color: #800080;">$path</span>="uploads"<span style="color: #000000;">;
</span><span style="color: #0000ff;">include_once</span> 'upFunc.php'<span style="color: #000000;">;
uploadFile(</span><span style="color: #800080;">$fileInfo</span>, <span style="color: #800080;">$path</span>, <span style="color: #800080;">$allowExt</span>, <span style="color: #800080;">$maxSize</span>);

 

三、多文件的上传实现

3.1 利用单文件封装

<span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=UTF-8"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Insert title here<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="doAction5.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile1"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile2"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile3"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile4"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">print_r($_FILES);</span>
<span style="color: #008080;">header</span>('content-type:text/html;charset=utf-8'<span style="color: #000000;">);
</span><span style="color: #0000ff;">include_once</span> 'upFunc.php'<span style="color: #000000;">;
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$_FILES</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$fileInfo</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$file</span>[]=uploadFile(<span style="color: #800080;">$fileInfo</span><span style="color: #000000;">);
}</span>

 

这里的思路,从print_r($_FILES)中去找,打印出来看到是个二维数组,很简单,遍历去用就好了! 

上面那个function的定义改一下,给定一些默认值

<span style="color: #0000ff;">function</span> uploadFile(<span style="color: #800080;">$fileInfo</span>,<span style="color: #800080;">$path</span>="uploads",<span style="color: #800080;">$allowExt</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','tif'),<span style="color: #800080;">$maxSize</span>=10485760){

 

这样子,简单是简单,但遇到一些问题。

正常的上传4个图片是没问题,但要是中间激活了函数中的exit,就会立即停止,导致其他图片也无法上传。

3.2 升级版封装

旨在实现针对多个或单个文件上传的封装

首先这样子写个静态文件

<span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=UTF-8"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Insert title here<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="doAction5.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile[]"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile[]"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile[]"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile[]"</span> <span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

 

打印一下$_FILES

<span style="color: #000000;">Array
(
    [myFile] => Array
        (
            [name] => Array
                (
                    [0] => test32.png
                    [1] => test32.png
                    [2] => 333.png
                    [3] => test41.png
                )

            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                    [2] => image/png
                    [3] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => D:\wamp\tmp\php831C.tmp
                    [1] => D:\wamp\tmp\php834C.tmp
                    [2] => D:\wamp\tmp\php837C.tmp
                    [3] => D:\wamp\tmp\php83BB.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [size] => Array
                (
                    [0] => 46174
                    [1] => 46174
                    [2] => 34196
                    [3] => 38514
                )

        )

)</span>

 

可以得到一个三维数组。

复杂是复杂了,但复杂的有规律,各项数值都在一起了,很方便我们取值!!

所以先得到文件信息,变成单文件处理那种信息

<span style="color: #0000ff;">function</span><span style="color: #000000;"> getFiles(){
    </span><span style="color: #800080;">$i</span>=0<span style="color: #000000;">;
    </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$_FILES</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$file</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_string</span>(<span style="color: #800080;">$file</span>['name'])){  <span style="color: #008000;">//</span><span style="color: #008000;">单文件判定</span>
            <span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]=<span style="color: #800080;">$file</span><span style="color: #000000;">;
            </span><span style="color: #800080;">$i</span>++<span style="color: #000000;">;
        }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$file</span>['name'<span style="color: #000000;">])){
            </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$file</span>['name'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span>=><span style="color: #800080;">$val</span>){  <span style="color: #008000;">//</span><span style="color: #008000;">我的天,这个$key用的diao</span>
                <span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]['name']=<span style="color: #800080;">$file</span>['name'][<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]['type']=<span style="color: #800080;">$file</span>['type'][<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]['tmp_name']=<span style="color: #800080;">$file</span>['tmp_name'][<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]['error']=<span style="color: #800080;">$file</span>['error'][<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$files</span>[<span style="color: #800080;">$i</span>]['size']=<span style="color: #800080;">$file</span>['size'][<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #800080;">$i</span>++<span style="color: #000000;">;
            }
        }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$files</span><span style="color: #000000;">;
    
}</span>

 

然后之前的那种exit错误,就把exit改一下就好了,这里用res

<span style="color: #0000ff;">function</span> uploadFile(<span style="color: #800080;">$fileInfo</span>,<span style="color: #800080;">$path</span>='./uploads',<span style="color: #800080;">$flag</span>=<span style="color: #0000ff;">true</span>,<span style="color: #800080;">$maxSize</span>=1048576,<span style="color: #800080;">$allowExt</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','gif'<span style="color: #000000;">)){
    </span><span style="color: #008000;">//</span><span style="color: #008000;">$flag=true;
    //$allowExt=array('jpeg','jpg','gif','png');
    //$maxSize=1048576;//1M
    //判断错误号</span>
    <span style="color: #800080;">$res</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$fileInfo</span>['error']===<span style="color: #000000;">UPLOAD_ERR_OK){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">检测上传得到小</span>
        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$fileInfo</span>['size']><span style="color: #800080;">$maxSize</span><span style="color: #000000;">){
            </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'上传文件过大'<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$ext</span>=getExt(<span style="color: #800080;">$fileInfo</span>['name'<span style="color: #000000;">]);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">检测上传文件的文件类型</span>
        <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$ext</span>,<span style="color: #800080;">$allowExt</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'非法文件类型'<span style="color: #000000;">;
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">检测是否是真实的图片类型</span>
        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$fileInfo</span>['tmp_name'<span style="color: #000000;">])){
                </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'不是真实图片类型'<span style="color: #000000;">;
            }
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">检测文件是否是通过HTTP POST上传上来的</span>
        <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$fileInfo</span>['tmp_name'<span style="color: #000000;">])){
            </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'文件不是通过HTTP POST方式上传上来的'<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span>) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$path='./uploads';</span>
        <span style="color: #0000ff;">if</span>(!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)){
            </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$path</span>,0777,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
            </span><span style="color: #008080;">chmod</span>(<span style="color: #800080;">$path</span>,0777<span style="color: #000000;">);
        }
        </span><span style="color: #800080;">$uniName</span>=<span style="color: #000000;">getUniName();
        </span><span style="color: #800080;">$destination</span>=<span style="color: #800080;">$path</span>.'/'.<span style="color: #800080;">$uniName</span>.'.'.<span style="color: #800080;">$ext</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$fileInfo</span>['tmp_name'],<span style="color: #800080;">$destination</span><span style="color: #000000;">)){
            </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'文件移动失败'<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$res</span>['mes']=<span style="color: #800080;">$fileInfo</span>['name'].'上传成功'<span style="color: #000000;">;
        </span><span style="color: #800080;">$res</span>['dest']=<span style="color: #800080;">$destination</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">;
        
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #008000;">//</span><span style="color: #008000;">匹配错误信息</span>
        <span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$fileInfo</span> ['error'<span style="color: #000000;">]) {
            </span><span style="color: #0000ff;">case</span> 1 :
                <span style="color: #800080;">$res</span>['mes'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 2 :
                <span style="color: #800080;">$res</span>['mes'] = '超过了表单MAX_FILE_SIZE限制的大小'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 3 :
                <span style="color: #800080;">$res</span>['mes'] = '文件部分被上传'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 4 :
                <span style="color: #800080;">$res</span>['mes'] = '没有选择上传文件'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 6 :
                <span style="color: #800080;">$res</span>['mes'] = '没有找到临时目录'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">case</span> 7 :
            <span style="color: #0000ff;">case</span> 8 :
                <span style="color: #800080;">$res</span>['mes'] = '系统错误'<span style="color: #000000;">;
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$res</span><span style="color: #000000;">;
    }
}</span>

 

里面封装了两个小的

<span style="color: #0000ff;">function</span> getExt(<span style="color: #800080;">$filename</span><span style="color: #000000;">){
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">strtolower</span>(<span style="color: #008080;">pathinfo</span>(<span style="color: #800080;">$filename</span>,<span style="color: #000000;">PATHINFO_EXTENSION));
}

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 产生唯一字符串
 * @return string
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> getUniName(){
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">md5</span>(<span style="color: #008080;">uniqid</span>(<span style="color: #008080;">microtime</span>(<span style="color: #0000ff;">true</span>),<span style="color: #0000ff;">true</span><span style="color: #000000;">));
}</span>

 

然后静态中,用multiple属性实现多个文件的输入;

<span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=UTF-8"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Insert title here<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="doAction6.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="POST"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
请选择您要上传的文件:</span><span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="myFile[]"</span><span style="color: #ff0000;"> multiple</span><span style="color: #0000ff;">='multiple' </span><span style="color: #0000ff;">/><span style="color: #800000;">br</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="上传"</span><span style="color: #0000ff;">/></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span>

 

doAction6

<span style="color: #000000;">php 
</span><span style="color: #008000;">//</span><span style="color: #008000;">print_r($_FILES);</span>
<span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
</span><span style="color: #0000ff;">require_once</span> 'upFunc2.php'<span style="color: #000000;">;
</span><span style="color: #0000ff;">require_once</span> 'common.func.php'<span style="color: #000000;">;
</span><span style="color: #800080;">$files</span>=<span style="color: #000000;">getFiles();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> print_r($files);</span>
<span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$files</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$fileInfo</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$res</span>=uploadFile(<span style="color: #800080;">$fileInfo</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$res</span>['mes'],'<br>'<span style="color: #000000;">;
    </span><span style="color: #800080;">$uploadFiles</span>[]=@<span style="color: #800080;">$res</span>['dest'<span style="color: #000000;">];
}
</span><span style="color: #800080;">$uploadFiles</span>=<span style="color: #008080;">array_values</span>(<span style="color: #008080;">array_filter</span>(<span style="color: #800080;">$uploadFiles</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">print_r($uploadFiles);</span>

 

这样子的几个文件,就实现比较强大的面向过程的上传文件的功能(学的叫一个心酸。。。);

四、面向对象的文件上传

 (不是很写的动了。。。先粘过来,再说吧。。。

<span style="color: #000000;">php 
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> upload{
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$fileName</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$maxSize</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$allowMime</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$allowExt</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$uploadPath</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$imgFlag</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$fileInfo</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$error</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$ext</span><span style="color: #000000;">;
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * @param string $fileName
     * @param string $uploadPath
     * @param string $imgFlag
     * @param number $maxSize
     * @param array $allowExt
     * @param array $allowMime
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$fileName</span>='myFile',<span style="color: #800080;">$uploadPath</span>='./uploads',<span style="color: #800080;">$imgFlag</span>=<span style="color: #0000ff;">true</span>,<span style="color: #800080;">$maxSize</span>=5242880,<span style="color: #800080;">$allowExt</span>=<span style="color: #0000ff;">array</span>('jpeg','jpg','png','gif'),<span style="color: #800080;">$allowMime</span>=<span style="color: #0000ff;">array</span>('image/jpeg','image/png','image/gif'<span style="color: #000000;">)){
        </span><span style="color: #800080;">$this</span>->fileName=<span style="color: #800080;">$fileName</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->maxSize=<span style="color: #800080;">$maxSize</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->allowMime=<span style="color: #800080;">$allowMime</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->allowExt=<span style="color: #800080;">$allowExt</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->uploadPath=<span style="color: #800080;">$uploadPath</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->imgFlag=<span style="color: #800080;">$imgFlag</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->fileInfo=<span style="color: #800080;">$_FILES</span>[<span style="color: #800080;">$this</span>-><span style="color: #000000;">fileName];
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 检测上传文件是否出错
     * @return boolean
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkError(){
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_null</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">fileInfo)){
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->fileInfo['error']>0<span style="color: #000000;">){
                </span><span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$this</span>->fileInfo['error'<span style="color: #000000;">]){
                    </span><span style="color: #0000ff;">case</span> 1:
                        <span style="color: #800080;">$this</span>->error='超过了PHP配置文件中upload_max_filesize选项的值'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 2:
                        <span style="color: #800080;">$this</span>->error='超过了表单中MAX_FILE_SIZE设置的值'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 3:
                        <span style="color: #800080;">$this</span>->error='文件部分被上传'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 4:
                        <span style="color: #800080;">$this</span>->error='没有选择上传文件'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 6:
                        <span style="color: #800080;">$this</span>->error='没有找到临时目录'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 7:
                        <span style="color: #800080;">$this</span>->error='文件不可写'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                    </span><span style="color: #0000ff;">case</span> 8:
                        <span style="color: #800080;">$this</span>->error='由于PHP的扩展程序中断文件上传'<span style="color: #000000;">;
                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
                        
                }
                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
            }
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$this</span>->error='文件上传出错'<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 检测上传文件的大小
     * @return boolean
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> checkSize(){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->fileInfo['size']><span style="color"></span>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。