Heim  >  Artikel  >  php教程  >  Thinkphp框架实现多文件上传实现代码

Thinkphp框架实现多文件上传实现代码

WBOY
WBOYOriginal
2016-05-25 16:44:52856Durchsuche

直接在php中的多文件上传我有介绍过很多并且也写过专题了,但是在thinkphp中多文件上传还是头一次碰到了,那么这个thinkphp多文件上传与php会有什么区别与共同点呢?下面我来给大家介绍一个Thinkphp框架实现多文件上传吧,希望对各位会有点帮助哦。

Thinkphp手册中对于多文件上传描述的很清楚:如果需要使用多个文件,只需要修改表单,把

<input type=&#39;file&#39; name=&#39;photo&#39;>

改为

<li><input type=&#39;file&#39; name=&#39;photo1&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo2&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo3&#39;></li>

或者

<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>


暂且自己的上传表单域为两个,一个上传图片,一个上传视频。字段名为image、video。

html代码如下

图片:<input type="file" name="image[]">
 
视频:<input type="file" name="video[]">

model代码:

<?php
protected $info = &#39;&#39;;
protected $_auto = array(
    array(
        &#39;image&#39;,
        &#39;upload&#39;,
        3,
        callback
    ) , //自动完成方法
    array(
        &#39;video&#39;,
        &#39;videoupload&#39;,
        3,
        callback
    ) , //自动完成方法
    
); //自动填充上传图片生成缩略图
protected function upload() {
    $var = $_FILES[&#39;image&#39;][&#39;name&#39;];
    import(&#39;ORG.Net.UploadFile&#39;);
    $upload = new UploadFile();
    $upload->saveRule = time;
    $upload->allowExts = array(
        &#39;jpg&#39;,
        &#39;gif&#39;,
        &#39;png&#39;,
        &#39;zip&#39;,
        &#39;flv&#39;
    );
    $upload->thumb = true;
    //视频路径。。。只支持flv后缀,
    $upload->videopath = &#39;./Public/upload/Video/&#39;;
    $upload->savePath = &#39;./Public/upload/images/&#39;;
    $upload->thumbPrefix = &#39;250_115_,150_110_,213_156_&#39;;
    $upload->thumbMaxWidth = &#39;250,150,213&#39;;
    $upload->thumbMaxHeight = &#39;115,110,156&#39;;
    if (!in_array(&#39;&#39;, $var) || !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        if (!$upload->upload()) {
            echo $upload->getErrorMsg();
            die;
        } else {
            $this->info = $upload->getUploadFileInfo();
            if (!in_array(&#39;&#39;, $var) & amp; & amp;
            !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
                return $this->info[1][&#39;savename&#39;];
            } elseif (!in_array(&#39;&#39;, $var)) {
                return $this->info[0][&#39;savename&#39;];
            } else {
                return false;
            }
        }
    } else {
        return flase;
    }
}
//上传视频
protected function videoupload() {
    if (!in_array(&#39;&#39;, $var) & amp; & amp;
    !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        return $this->info[0][&#39;savename&#39;];
    } elseif (!in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        return $this->info[1][&#39;savename&#39;];
    } else {
        return false;
    }
}
?>

文章最后我来分析一下多文件上传原理吧,先来看看html代码

<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>

这种就是把表单变量定义为数组,在php中数组特殊变量它可以存储多个不定长的内容,所以我们就可以自定多文件上传框了,那么在php处理时我们要如何操作,下面看例子。

protected $_auto = array(
array(&#39;image&#39;,&#39;upload&#39;,3,callback),//自动完成方法
array(&#39;video&#39;,&#39;videoupload&#39;,3,callback), //自动完成方法
);//自动填充上传图片生成缩略图

这个是告诉thinkphp是数组变量了,并不需要像原生态的php中来判断遍历数组长度再一个个上传的代码了,因为了thinkphp己经做好了。

本文地址:

转载随意,但请附上文章地址:-)

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn