php进阶——02 多文件上传

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lihaoen666/article/details/80611099

前言

使用MVC的思想去封装一个多文件上传类,入口文件为index.php,视图文件有single和group2个html文件,controller有upload.class.php。



index.php

// 1.定义根目录常量FILEROOT
                // 把路径名里的'\\'换成'/'
define("FILEROOT",str_replace('\\','/',dirname(__FILE__)));


// 2,引入文件上传处理类
require_once FILEROOT.'/controller/Upload.class.php';

// 3.实例化上传对象
$upload = new Upload();

// 4.初始化action参数
//   参数为空 或者 不属于[show,upload],设为show,否则设为upload
$action = empty($_GET['action']) || !in_array($_GET['action'],array('show','upload')) ? 'show' : $_GET['action'];


// 5.如果show,就显示页面;如果是upload,就处理用户上传文件
if($action == 'show'){
    $type = empty($_GET['type']) || !in_array($_GET['type'],array('single','group'))
          ?'single':$_GET['type'];
    $upload -> showPage($type);

}elseif($action == 'upload'){
    $upload -> handleUpload();
}



single.html(独立名称)

<form action="index.php?action=upload" method="post" enctype="multipart/form-data">
    <!--  这个用来识别是独立名称,还是名称组-->
    <input type="hidden" name="type" value="single">

    <!-- name不一样 -->
    <div class="form-group">one
        <input type="file" name="one" class="form-control">
    </div>
    <div class="form-group">two
        <input type="file" name="two" class="form-control">
    </div>
    <div class="form-group">three
        <input type="file" name="three" class="form-control">
    </div>
    <div class="form-group">
        <input type="submit" value="上传" class="btn btn-primary">
    </div>
 </form>  



group.html(名称组)

<!-- 注意,这里的action在问号后带参数 -->
<form action="index.php?action=upload" method="post" enctype="multipart/form-data">
    <!--  这个用来识别是独立名称,还是名称组-->
    <input type="hidden" name="type" value="group">

    <!-- name一样 -->
    <div class="form-group">upload[]
        <input type="file" name="upload[]" class="form-control">
    </div>
    <div class="form-group">upload[]
        <input type="file" name="upload[]" class="form-control">
    </div>
    <div class="form-group">upload[]
        <input type="file" name="upload[]" class="form-control">
    </div>
    <div class="form-group">
        <input type="submit" value="上传" class="btn btn-primary">
    </div>
</form>



独立名称、名称组的区别


独立名称$_FILES数据结构

这里写图片描述


名称组$_FILES数据结构

这里写图片描述



upload.class.php

// 文件上传处理类
class Upload
{
    // 1.用于保存多个文件的信息
    private $file = array();

    // 2.保存上传结果
    private $return;

    // 3.显示上传页面函数
    public function showPage($templateName)
    {
        echo file_get_contents(FILEROOT.'/view/'.$templateName.'.html');
    }

    // 4.处理上传文件主函数
    public function handleUpload()
    {
        // 获取文件信息并处理
        $this -> fixFiles();
    }

    // 5.获取上传文件信息并处理 函数
    private function fixFiles()
    {
        // 独立名称
        if($_POST['type'] == 'single'){
            $this -> file = $_FILES;

        // 名称组
        }else{
            foreach($_FILES as $name => $info){
                foreach($info as $attr => $values){
                    foreach($info as $attr => $values){
                        $this->file = [$name.$index]['attr'] = $value;
                    }
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/lihaoen666/article/details/80611099
今日推荐