首頁  >  文章  >  後端開發  >  PHP多文件上传操作,_PHP教程

PHP多文件上传操作,_PHP教程

WBOY
WBOY原創
2016-07-13 10:21:43820瀏覽

PHP多文件上传操作,

在前一篇文章里讲到了关于PHP文件上传原理和简单操作举例是单文件上传。

http://www.cnblogs.com/lichenwei/p/3879566.html

其实多文件上传和单文件上传大同小异,原理都是一样的,只是在代码上做了点小技巧。

 

首先还是index.html上传表单,只是把之前上传文件表单里的file更改成了file[]

<span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span>
<span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span> xml:lang</span><span>="en"</span><span>></span>
<span><</span><span>head</span><span>></span>
    <span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html;charset=UTF-8"</span><span>></span>
    <span><</span><span>title</span><span>></span>upload files<span></</span><span>title</span><span>></span>
<span></</span><span>head</span><span>></span>
<span><</span><span>body</span><span>></span>

    <span><</span><span>form </span><span>action</span><span>="upload.php"</span><span> enctype</span><span>="multipart/form-data"</span><span> method</span><span>="post"</span><span>></span>
        <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="100000"</span> <span>/></span><span>
        上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span><span>
        上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span><span>
        上传文件:</span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file[]"</span><span>/><</span><span>br</span><span>/></span>    
        <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="上传"</span> <span>/></span>
    <span></</span><span>form</span><span>></span>
<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

在upload.php用$_FILES打印看看

<?<span>php  <br /></span><span>  print_r</span>(<span>$_FILES</span>); <br />?>

得出下面多维数组

<span>Array</span><span>
(
    [</span><span>file</span>] => <span>Array</span><span>
        (
            [name] </span>=> <span>Array</span><span>
                (
                    [</span>0] => 照片1.<span>jpg
                    [</span>1] => 照片2.<span>jpg
                    [</span>2] => 照片3.<span>jpg
                )

            [type] </span>=> <span>Array</span><span>
                (
                    [</span>0] => image/<span>jpeg
                    [</span>1] => image/<span>jpeg
                    [</span>2] => image/<span>jpeg
                )

            [tmp_name] </span>=> <span>Array</span><span>
                (
                    [</span>0] => F:\wamp\tmp\php36C7.<span>tmp
                    [</span>1] => F:\wamp\tmp\php36C8.<span>tmp
                    [</span>2] => F:\wamp\tmp\php36C9.<span>tmp
                )

            [error] </span>=> <span>Array</span><span>
                (
                    [</span>0] => 0<span>
                    [</span>1] => 0<span>
                    [</span>2] => 0<span>
                )

            [size] </span>=> <span>Array</span><span>
                (
                    [</span>0] => 0<span>
                    [</span>1] => 0<span>
                    [</span>2] => 0<span>
                )

        )

)</span>

按照单文件上传的原理,先想想我们需要得到什么?

很明显我们需要得到一个关于文件信息的数组,数组里包含name,type,tmp_name,error,size,而此时我们得到的是个多维数组,虽然对应的键值都存在,但它是多维的,

我们只需要把它拆分,比如上面的3个文件,我们只需要把它拆分成对应的3个文件信息数组就行了。

拆分数组的结构

<span>Array</span><span>
(
    [</span>0] => <span>Array</span><span>
        (
            [name] </span>=> 照片1.<span>jpg
            [type] </span>=> image/<span>jpeg
            [tmp_name] </span>=> F:\wamp\tmp\php13C1.<span>tmp
            [error] </span>=> 0<span>
            [size] </span>=> 385150<span>
        )

    [</span>1] => <span>Array</span><span>
        (
            [name] </span>=> 照片2.<span>jpg
            [type] </span>=> image/<span>jpeg
            [tmp_name] </span>=> F:\wamp\tmp\php13D2.<span>tmp
            [error] </span>=> 0<span>
            [size] </span>=> 242043<span>
        )

    [</span>2] => <span>Array</span><span>
        (
            [name] </span>=> 照片3.<span>jpg
            [type] </span>=> image/<span>jpeg
            [tmp_name] </span>=> F:\wamp\tmp\php13D3.<span>tmp
            [error] </span>=> 0<span>
            [size] </span>=> 488293<span>
        )

)</span>

下面是拆分重组数组代码

<?<span>php
    

    </span><span>//</span><span>print_r($_FILES['file']);</span>
    <span>$arr</span>=<span>$_FILES</span>['file'<span>];
    </span><span>$files</span>=<span>array</span><span>();
    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$arr</span>['name']);<span>$i</span>++<span>){
        </span><span>$files</span>[<span>$i</span>]['name']=<span>$arr</span>['name'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['type']=<span>$arr</span>['type'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['tmp_name']=<span>$arr</span>['tmp_name'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['error']=<span>$arr</span>['error'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['size']=<span>$arr</span>['size'][<span>$i</span><span>];
    }

    </span><span>print_r</span>(<span>$files</span>);<br />?>

 

剩下的东西就简单了,重复单文件上传的步骤,遍历处理一遍该数组就行了。

代码如下:

<?<span>php
    

    </span><span>//</span><span>print_r($_FILES['file']);</span>
    <span>$arr</span>=<span>$_FILES</span>['file'<span>];
    </span><span>$files</span>=<span>array</span><span>();
    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$arr</span>['name']);<span>$i</span>++){<span>//</span><span>count()统计数组键值name长度</span>
        <span>$files</span>[<span>$i</span>]['name']=<span>$arr</span>['name'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['type']=<span>$arr</span>['type'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['tmp_name']=<span>$arr</span>['tmp_name'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['error']=<span>$arr</span>['error'][<span>$i</span><span>];
        </span><span>$files</span>[<span>$i</span>]['size']=<span>$arr</span>['size'][<span>$i</span><span>];
    }

    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$files</span>);<span>$i</span>++<span>){
    </span><span>//</span><span>取得上传文件信息</span>
    <span>$fileName</span>=<span>$files</span>[<span>$i</span>]['name'<span>];
    </span><span>$fileType</span>=<span>$files</span>[<span>$i</span>]['type'<span>];
    </span><span>$fileError</span>=<span>$files</span>[<span>$i</span>]['type'<span>];
    </span><span>$fileSize</span>=<span>$files</span>[<span>$i</span>]['size'<span>];
    </span><span>$tempName</span>=<span>$files</span>[<span>$i</span>]['tmp_name'];<span>//</span><span>临时文件名
    
    //定义上传文件类型</span>
    <span>$typeList</span> = <span>array</span>("image/jpeg","image/jpg","image/png","image/gif"); <span>//</span><span>定义允许的类型</span>

    <span>if</span>(<span>$fileError</span>>0<span>){
            </span><span>//</span><span>上传文件错误编号判断</span>
            <span>switch</span> (<span>$fileError</span><span>) {
                </span><span>case</span> 1:
                    <span>$message</span>="上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值。"<span>; 
                    </span><span>break</span><span>;
                </span><span>case</span> 2:
                    <span>$message</span>="上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"<span>; 
                    </span><span>break</span><span>;
                </span><span>case</span> 3:
                    <span>$message</span>="文件只有部分被上传。"<span>; 
                    </span><span>break</span><span>;
                </span><span>case</span> 4:
                    <span>$message</span>="没有文件被上传。"<span>;
                    </span><span>break</span><span>;
                </span><span>case</span> 6:
                    <span>$message</span>="找不到临时文件夹。"<span>; 
                    </span><span>break</span><span>;
                </span><span>case</span> 7:
                    <span>$message</span>="文件写入失败"<span>; 
                    </span><span>break</span><span>;
                </span><span>case</span> 8:
                    <span>$message</span>="由于PHP的扩展程序中断了文件上传"<span>;
                    </span><span>break</span><span>;
            }

            </span><span>exit</span>("文件上传失败:".<span>$message</span><span>);

        }
    </span><span>if</span>(!<span>is_uploaded_file</span>(<span>$tempName</span><span>)){
        </span><span>//</span><span>判断是否是POST上传过来的文件</span>
        <span>exit</span>("不是通过HTTP POST方式上传上来的"<span>);
    }</span><span>else</span><span>{
        </span><span>if</span>(!<span>in_array</span>(<span>$fileType</span>, <span>$typeList</span><span>)){
            </span><span>exit</span>("上传的文件不是指定类型"<span>);
        }</span><span>else</span><span>{
            </span><span>if</span>(!<span>getimagesize</span>(<span>$tempName</span><span>)){
                </span><span>//</span><span>避免用户上传恶意文件,如把病毒文件扩展名改为图片格式</span>
                <span>exit</span>("上传的文件不是图片"<span>);
            }
        }
            </span><span>if</span>(<span>$fileSize</span>>1000000<span>){
                </span><span>//</span><span>对特定表单的上传文件限制大小</span>
                <span>exit</span>("上传文件超出限制大小"<span>);
            }</span><span>else</span><span>{
                </span><span>//</span><span>避免上传文件的中文名乱码</span>
                <span>$fileName</span>=<span>iconv</span>("UTF-8", "GBK", <span>$fileName</span>);<span>//</span><span>把iconv抓取到的字符编码从utf-8转为gbk输出</span>
                <span>$fileName</span>=<span>str_replace</span>(".", <span>time</span>().".", <span>$fileName</span>);<span>//</span><span>在图片名称后加入时间戳,避免重名文件覆盖</span>
                <span>if</span>(<span>move_uploaded_file</span>(<span>$tempName</span>, "uploads/".<span>$fileName</span><span>)){
                    </span><span>echo</span> "上传文件成功!"<span>;
                }</span><span>else</span><span>{
                    </span><span>echo</span> "上传文件失败"<span>;
                }
            }

        }

    }
</span>?>

 

效果如下:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/853717.htmlTechArticlePHP多文件上传操作, 在前一篇文章里讲到了关于PHP文件上传原理和简单操作举例是单文件上传。 http://www.cnblogs.com/lichenwei/p/3879566.html 其实...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn